Home > Article > Backend Development > How to delete duplicate elements in an array in php
The way PHP deletes duplicate elements in an array is through the built-in function array_unique(). The array_unique() function is used to remove duplicate values from the array and return the filtered array. Function syntax: [array_unique(array)].
To remove duplicate elements in an array, you can use the built-in function array_unique().
(Recommended learning: php tutorial)
Introduction to related functions:
array_unique() function is used to remove duplicate values from the array. If two or more array values are the same, only the first value is retained and the other values are removed.
Note: The retained array will retain the key type of the first array item.
Syntax:
array_unique(array)
Return value:
Return the filtered array.
Code implementation:
<?php $a=array("a"=>"red","b"=>"green","c"=>"red"); print_r(array_unique($a)); ?>
Run result:
Array ( [a] => red [b] => green )
The above is the detailed content of How to delete duplicate elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!