Google

Saturday, January 23, 2010

How to get unique value form PHP array?


array_unique()


(PHP 4 >= 4.0.1, PHP 5)

array_unique — Removes duplicate values from an array

Description:

Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

Example:


/*= array("a" => "1", "2", "3" => "1", "2", "3");
$result = array_unique($input);
print_r($result);
*/

Result:
/*Array
(
[a] => 1
[0] => 2
[2] => 3
)*/