本文目錄一覽:
如何正確實現PHP刪除數組重複元素
array_unique
(PHP 4 = 4.0.1, PHP 5, PHP 7)
array_unique — 移除數組中重複的值
說明
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array_unique() 接受 array 作為輸入並返回沒有重複值的新數組。
注意鍵名保留不變。array_unique() 先將值作為字符串排序,然後對每個值只保留第一個遇到的鍵名,接着忽略所有後面的鍵名。這並不意味着在未排序的 array 中同一個值的第一個出現的鍵名會被保留。
Note: 當且僅當 (string) $elem1 === (string) $elem2 時兩個單元被認為相同。就是說,當字符串的表達一樣時。 第一個單元將被保留。
參數
array
輸入的數組。
sort_flags
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
Sorting type flags:
SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings
SORT_LOCALE_STRING – compare items as strings, based on the current locale.
返回值
Returns the filtered array.
更新日誌
版本
說明
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
範例
Example #1 array_unique() 例子
?php
$input = array(“a” = “green”, “red”, “b” = “green”, “blue”, “red”);
$result = array_unique($input);
print_r($result);
?
以上例程會輸出:
Array
(
[a] = green
[0] = red
[1] = blue
)
Example #2 array_unique() 和類型
?php
$input = array(4, “4”, “3”, 4, 3, “3”);
$result = array_unique($input);
var_dump($result);
?
以上例程會輸出:
array(2) {
[0] = int(4)
[2] = string(1) “3”
}
參見
array_count_values() – 統計數組中所有的值出現的次數
注釋
Note: Note that array_unique() is not intended to work on multi dimensional arrays.
php 如何去除多維數組指定重複出現的值
array_unique
()
函數用於移除數組中重複的值。如果兩個或更多個數組值相同,只保留第一個值,其他的值被移除。
注意:被保留的數組將保持第一個數組項的鍵名類型
php 刪除數組重複的值
$arr = array(1,2,4,2,0,9,8,5);//定義一個數組。
$arr1 = $arr; //定義另一個數組和上一個數組一樣。
//循環第一個數組讓後循環第二個數組 用第一個數組的每個值和第二個數組比較如果相同就刪除,最後輸出第二個數組就行了。
for($i = 0;$icount($arr);$i++){
for($j=$i+1;$jcount($arr);$j++){
if($arr[$i] == $arr[$j])
unset($arr1[$i]);
}
}
echo’pre’;
print_r($arr1);
第二 如果允許使用array_uniqe()函數的話,直接array_uniqe(直接寫數組名就ok)。
在PHP中可以使用內置函數array_unique()來直接刪除重複元素,也可以使用array_flip()函數來間接刪除重複元素。
1.array_unique()函數
array_unique()函數可以移除數組中的重複的值,並返回結果數組;當幾個數組元素的值相等時,只保留第一個元素,其他的元素被刪除。
代碼示例:
?php$result1 = array(“a” = “green”, “red”, “b” = “green”, “blue”,
“red”);var_dump($result1);$result2 = array_unique($result1);var_dump($result2);?
2.array_flip()函數
array_flip()是反轉數組鍵和值的函數,它有個特性就是如果數組中有二個值是一樣的,那麼反轉後會保留最後一個鍵和值,利用這個特性我們用他來間接的實現數組的去重。
代碼示例:
?phpheader(“content-type:text/html;
charset=utf-8″);$a = array(1, 5, 2, 5, 1, 3, 2, 4, 5);// 輸出原始數組echo “原始數組
:”;var_dump($a);// 。
通過使用翻轉鍵和值移除重複值$a = array_flip($a);
// 通過再次翻轉鍵和值來恢複數組元素$a = array_flip($a);// 重新排序數組鍵$a = array_values($a);// 輸出更新後的數組echo “更新數組 :”;var_dump($a);?
擴展資料:
在 PHP 中創建數組:
在 PHP 中, array() 函數用於創建數組:
array();
在 PHP 中,有三種數組類型:
索引數組 – 帶有數字索引的數組。
關聯數組 – 帶有指定鍵的數組。
多維數組 – 包含一個或多個數組的數組。
1、PHP 索引數組
有兩種創建索引數組的方法:
索引是自動分配的(索引從 0 開始):
$cars=array(“porsche”,”BMW”,”Volvo”);
或者也可以手動分配索引:
$cars[0]=”porsche”;
2、遍歷索引數組:
如需遍歷並輸出索引數組的所有值,可以使用 for 循環,就像這樣:
實例:
?php
$cars=array(“porsche”,”BMW”,”Volvo”);
$arrlength=count($cars);
for($x=0;$x$arrlength;$x++) {
echo $cars[$x];
echo “br”;
}
?
3、多維數組:
將在 PHP 高級教程出現多維數組。
參考資料來源:百度百科-PHP
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/246377.html