Array_unique (PHP 4> = 4.0.1, PHP 5)
Array_unique -- remove repeated values from the array
Description
Array array_unique (array)
Array_unique () accepts array as input and returns a new array without repeated values.
Note that the key name remains unchanged. Array_unique () First sorts the values as strings, then retains only the first key name that is encountered for each value, and then ignores all the key names that follow. This does not mean that the first key name that appears for the same value in the unordered array will be retained.
Note: When and only when (string) $ elem1 = (string) $ elem2, the two units are considered the same. That is, when the expression of a string is the same.
The first unit will be retained.
Example 1. array_unique () Example
CopyCode The Code is as follows: <? PHP
$ Input = array ("A" => "green", "Red", "B" => "green", "blue", "Red ");
$ Result = array_unique ($ input );
Print_r ($ result );
?>
The above example will output:
Copy code The Code is as follows: Array
(
[A] => green
[0] => Red
[1] => blue
)
Example 2. array_unique () and type
The above example will output:
Copy code The Code is as follows: <? PHP
$ Input = array (4, "4", "3", 4, 3, "3 ");
$ Result = array_unique ($ input );
Var_dump ($ result );
?>
Copy code The Code is as follows: array (2 ){
[0] => int (4)
[2] => string (1) "3"
}