Home >Backend Development >PHP Problem >How to remove identical values from php array
In php, you can use the array_unique() function to remove the same values in the array, the syntax format is "array_unique(array)". If there are two or more values in the array that are the same, use the array_unique() function to keep only the first value and the other values will be removed.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
Remove the same values from the php array
In PHP, you can use the array_unique() function to remove identical values from an array. Example:
<?php $arr1 =array("a"=>"Cat","b"=>"Dog","c"=>"Cat"); var_dump($arr1); $arr2=array_unique($arr1); var_dump($arr2); ?>
Output:
Related function introduction:
array_unique() function shift Removes duplicate values in an array and returns the resulting array. When the values of several array elements are equal, only the first element is retained and the other elements are deleted. The key names in the returned array remain unchanged.
Grammar:
array_unique(array)
[Recommended learning: "PHP Video Tutorial"]
The above is the detailed content of How to remove identical values from php array. For more information, please follow other related articles on the PHP Chinese website!