Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

array_unique(3) [php man page]

ARRAY_UNIQUE(3) 							 1							   ARRAY_UNIQUE(3)

array_unique - Removes duplicate values from an array

SYNOPSIS
array array_unique (array $array, [int $sort_flags = SORT_STRING]) DESCRIPTION
Takes an input $array and returns a new array without duplicate values. Note that keys are preserved. array_unique(3) 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. Note Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation is the same, the first element will be used. PARAMETERS
o $array - The input array. o $sort_flags - The optional second parameter $sort_flags may be used to modify the sorting behavior using these values: Sorting type flags: o SORT_REGULAR - compare items normally (don't change types) o SORT_NUMERIC - compare items numerically o SORT_STRING - compare items as strings o SORT_LOCALE_STRING - compare items as strings, based on the current locale. RETURN VALUES
Returns the filtered array. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ |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. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 array_unique(3) example <?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?> The above example will output: Array ( [a] => green [0] => red [1] => blue ) Example #2 array_unique(3) and types <?php $input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input); var_dump($result); ?> The above example will output: array(2) { [0] => int(4) [2] => string(1) "3" } SEE ALSO
array_count_values(3). NOTES
Note Note that array_unique(3) is not intended to work on multi dimensional arrays. PHP Documentation Group ARRAY_UNIQUE(3)

Check Out this Related Man Page

ARRAY_MERGE(3)								 1							    ARRAY_MERGE(3)

array_merge - Merge one or more arrays

SYNOPSIS
array array_merge (array $array1, [array $...]) DESCRIPTION
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. PARAMETERS
o $array1 - Initial array to merge. o $... - Variable list of arrays to merge. RETURN VALUES
Returns the resulting array. EXAMPLES
Example #1 array_merge(3) example <?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?> The above example will output: Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Example #2 Simple array_merge(3) example <?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?> Don't forget that numeric keys will be renumbered! Array ( [0] => data ) If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator: <?php $array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = $array1 + $array2; var_dump($result); ?> The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored. array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" } Example #3 array_merge(3) with non-array types <?php $beginning = 'foo'; $end = array(1 => 'bar'); $result = array_merge((array)$beginning, (array)$end); print_r($result); ?> The above example will output: Array ( [0] => foo [1] => bar ) SEE ALSO
array_merge_recursive(3), array_replace(3), array_combine(3), array operators. PHP Documentation Group ARRAY_MERGE(3)
Man Page