The values of the array can be repeated. In PHP, the key of an array is unique and cannot exist repeatedly, but the key value of an array can exist repeatedly. Methods to deal with duplicate values: 1. Use the array_unique() function to delete duplicate values in the array; 2. Use the array_count_values() function to count the number of occurrences of all values in the array and return an associative array with the keys of its elements. The name is the value of the original array, and the key value is the number of times the value appears in the original array.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, the key of the array The name (key) is unique and cannot exist repeatedly, but the key value (value) of the array can exist repeatedly.
$arr=array("A","Cat","Dog","A","Dog","a",3,4);
As you can see, in the above example, the element values "A" and "Dog" have duplicate values.
Handling functions for duplicate values in PHP arrays
For duplicate values, PHP provides a variety of built-in processing functions, two of which are introduced below:
array_unique()
array_count_values()
##1. array_unique() function
array_unique() function is used to remove duplicate values in 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.2. array_count_values() function
array_count_values() function can count the number of occurrences of all values in the array; if the number If it is 1, the element is not repeated. array_count_values() function will return an associative array, the key name of its element is the value of the original array, and the key value is the number of times the value appears in the original array.PHP Video Tutorial"
The above is the detailed content of Can the values of an array in php be repeated?. For more information, please follow other related articles on the PHP Chinese website!