Home >Backend Development >PHP Problem >How to remove duplicate values in php
In PHP, you can use the "array_unique()" function to remove duplicate values. The function of this function is that when the values of several array elements are equal, only the first element is retained, and the other elements are deleted. The syntax is "array_unique(array)".
php remove duplicate values
array_unique() function definition and usage
## The #array_unique() function is used to remove duplicate values from an 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. Syntaxarray_unique(array)Parameters array required. Specifies an array. sortingtype Optional. Specifies the sorting type. Possible values: SORT_STRING - Default. Treat each item as a string. SORT_REGULAR - Sort each item in regular order (Standard ASCII, does not change type). SORT_NUMERIC - Treat each item as a number. SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
Example:
<?php $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat"); print_r(array_unique($a)); ?>Output:
Array ( [a] => Cat [b] => Dog )Recommended: "
PHP Tutorial"
The above is the detailed content of How to remove duplicate values in php. For more information, please follow other related articles on the PHP Chinese website!