Home > Article > Backend Development > php remove duplicates from array
In PHP development, array deduplication is a problem that often needs to be solved. When we need to process a large amount of data, how to efficiently remove duplicates in an array becomes a very important task. In this article, we will explain how to deduplicate arrays in PHP using different methods, as well as the pros and cons of these methods.
Method 1: Use array_unique function
The array_unique function is a way to handle array deduplication in PHP's built-in function. Its function is to return a new array containing the deduplicated elements in the original array.
Give an example to illustrate:
$fruits = array("apple", "banana", "orange", "banana", "pear"); $unique_fruits = array_unique($fruits); print_r($unique_fruits);
Output:
Array ( [0] => apple [1] => banana [2] => orange [4] => pear )
As you can see, after using the array_unique function, the duplicates in the original array are removed. However, it should be noted that this function can only handle arrays of value types and cannot handle arrays containing complex data types.
Method 2: Use loop traversal
In PHP, using a loop to traverse an array to remove duplicates is also a common way. Its principle is to traverse each element in the array and delete duplicate elements from the array.
The following is an example of using loop traversal to remove duplicates:
$fruits = array("apple", "banana", "orange", "banana", "pear"); for ($i = 0; $i < count($fruits); $i++) { for ($j = $i+1; $j < count($fruits); $j++) { if ($fruits[$i] == $fruits[$j]) { array_splice($fruits, $j, 1); $j--; } } } print_r($fruits);
Output:
Array ( [0] => apple [1] => banana [2] => orange [3] => pear )
It can be seen that the duplication in the array has been removed using loop traversal. item. However, this method also has some disadvantages:
Method 3: Use array_flip and array_keys functions
The array_flip function can exchange the keys and values of the array, and the array_keys function is used to obtain all the key names in the array. These two functions can easily remove duplicates from an array.
The following is an example:
$fruits = array("apple", "banana", "orange", "banana", "pear"); $unique_fruits = array_keys(array_flip($fruits)); print_r($unique_fruits);
Output:
Array ( [0] => apple [1] => banana [2] => orange [3] => pear )
As you can see, duplicates in the array can also be removed using this method. Compared with method two, this method is more efficient and does not modify the key-value pairs.
Method 4: Use array_reduce function
The array_reduce function is a functional programming idea, which can perform customized operations on an array and return a value.
The following is an example of using the array_reduce function to remove duplicates:
$fruits = array("apple", "banana", "orange", "banana", "pear"); $unique_fruits = array_reduce($fruits, function($result, $value) { if (!in_array($value, $result)) { $result[] = $value; } return $result; }, array()); print_r($unique_fruits);
Output:
Array ( [0] => apple [1] => banana [2] => orange [3] => pear )
As you can see, the array_reduce function can also be used to easily remove duplicates in an array. Duplicates. The advantage of this method compared to method two is that it has less code and fast execution speed.
Conclusion
In PHP development, deduplicating arrays is a basic operation, and there are many different ways to implement it. By comparing four different deduplication methods, we can draw the following conclusions: The
According to specific project needs, you can choose the most suitable deduplication method to improve code efficiency.
The above is the detailed content of php remove duplicates from array. For more information, please follow other related articles on the PHP Chinese website!