(PHP 4 >= 4.0.1, PHP 5)
array_unique — 移除數(shù)組中重復的值
說明
array array_unique ( array$array
[, int $sort_flags
= SORT_STRING
] )
array_unique() 接受
array
作為輸入并返回沒有重復值的新數(shù)組。
注意鍵名保留不變。array_unique()
先將值作為字符串排序,然后對每個值只保留第一個遇到的鍵名,接著忽略所有后面的鍵名。這并不意味著在未排序的
array
中同一個值的第一個出現(xiàn)的鍵名會被保留。
Note: 當且僅當 (string) $elem1 === (string) $elem2 時兩個單元被認為相同。就是說,當字符串的表達一樣時。 第一個單元將被保留。
參數(shù)
array
輸入的數(shù)組。
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() - 統(tǒng)計數(shù)組中所有的值出現(xiàn)的次數(shù)
注釋
Note: Note that array_unique() is not intended to work on multi dimensional arrays.