How to detect duplicate values in PHP array? Use array_unique() function to removes duplicate values from an array. It takes an input array and returns a new array without duplicate values.
Example #1 array_unique() example
1 2 3 4 5 | <?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?> |
The above example will output:
1 2 3 4 5 6 | Array ( [a] => green [0] => red [1] => blue ) |
Example #2 array_unique() and types
1 2 3 4 5 | <?php $input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input); var_dump($result); ?> |
Output:
1 2 3 4 | array(2) { [0] => int(4) [2] => string(1) "3" } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.