Sunday, July 1, 2012

Get Unique Value from PHP Array

array_unique()

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:

/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
$_input = array("a" => "1", "2", "3" => "1", "2", "3");
$result = array_unique($_input);
print_r($result);


Result:

Array
(
  [a] => 1
  [0] => 2
  [2] => 3
)
?>
 

No comments:

Post a Comment