array_unique

(PHP 4 >= 4.0.1)

array_unique -- Removes duplicate values from an array

Description

array array_unique (array array)

array_unique() takes input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() will keep the first key encountered for every value, and ignore all following keys.

Example 1. array_unique() example


$input = array ("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique ($input);
print_r($result);
// this will output :
//Array
//(
//    [a] => green
//    [0] => red
//    [1] => blue
//)
      

Note that array_unique() take into account value's type. This is usually of no matter, except when it comes to compare numbers, which can be of several types. This may lead to confusing results.

Example 2. array_unique() and types


$input = array (4,"3",3,"4",4,4);
$result = array_unique ($input);
print_r($result);
// this will output :
//Array
//(
//  [0] => 3
//  [1] => 3
//  [2] => 4
//  [3] => 4
//)