PHP is a server-side programming language that is commonly used for web development, dynamic web page programming, etc. In order to facilitate PHP programmers to operate data, PHP provides array Array as a data type. It is an ordered collection of key-value pairs and is also one of the most commonly used data structures in PHP. In actual development, arrays often encounter duplicate elements. To solve this problem, we need to start from the perspective of array deduplication. This article will introduce how to delete duplicate elements in PHP arrays.
1. Use the array_unique() function to remove duplicates
PHP provides a built-in function array_unique(), which is used to remove duplicate elements from the array and return a new array that does not contain duplicate elements. . The syntax of this function is as follows:
array array_unique(array $array[, int $sort_flags = SORT_STRING])
Among them, $array is the array that needs to be operated, and $sort_flags is an optional parameter that specifies the sorting method when deduplicating. If this parameter is not passed in, the string sorting method will be used by default. If you want to sort by numerical size, you can pass in the constant SORT_NUMERIC. The following is a sample code:
$old_array = [1, 3, 5, 3, 1, 6, 5]; $new_array = array_unique($old_array); var_dump($new_array);
The output result is:
array(4) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [5]=> int(6) }
It can be seen that after deduplication processing, duplicate elements are removed, leaving different elements. It should be noted that the key names of the new array are not consecutive, because according to PHP's syntax, integers and strings are two different data types.
2. Use loops to remove duplicates
In addition to the built-in function array_unique(), we can also use loops to achieve the duplicate function. The main idea is to iterate through each element of the array. If the element already exists in the new array, do not add it; otherwise, add it to the new array. The following is a sample code:
$old_array = [1, 3, 5, 3, 1, 6, 5]; $new_array = []; foreach ($old_array as $value) { if (!in_array($value, $new_array)) { $new_array[] = $value; } } var_dump($new_array);
The output result is:
array(4) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(6) }
It can be seen that after loop processing, the elements in the new array are the same as the output results of the built-in function, and duplicate elements are removed. , retaining different elements. It should be noted that the in_array() function is used inside the loop to determine whether the element already exists in the array. The return value of this function is a Boolean value.
3. Use array_flip() function to deduplicate arrays
In addition to array_unique() and loops, you can also use array_flip() function to deduplicate arrays. What this function does is swap the positions of the keys and values in the array and return a new array. Because the key names of arrays in PHP must be unique, you can remove duplicate elements from the array by using this function. The following is a sample code:
$old_array = [1, 3, 5, 3, 1, 6, 5]; $new_array = array_flip($old_array); var_dump(array_keys($new_array));
The output result is:
array(4) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(6) }
As you can see, after using the array_flip() function to remove duplicates, a new array containing only different elements is returned. But it should be noted that the values in the original array will be used as the key names of the new array, and the value of the new array will be automatically assigned a value of 1.
Conclusion
This article introduces three methods for deleting duplicate elements in PHP arrays: the built-in function array_unique(), loop and array_flip() function. Among them, the built-in function array_unique() is the simplest way, while the loop and array_flip() functions are more flexible and suitable for handling more special or complex situations. No matter which method is used, the key point is that the importance of array deduplication cannot be ignored. Only by ensuring that there are no duplicate elements can we better use arrays to complete various operations.
The above is the detailed content of Let's talk about how to delete duplicate elements in PHP arrays. For more information, please follow other related articles on the PHP Chinese website!