Home > Article > Backend Development > php array exclude duplicates
In PHP, it is often necessary to process array operations, and sometimes there are duplicate elements in an array that need to be eliminated. This article will introduce several commonly used methods to eliminate duplicates in PHP arrays.
The array_unique() function can return a new array with no duplicate values.
Sample code:
$arr = array("apple", "banana", "cherry", "apple", "banana"); $new_arr = array_unique($arr); print_r($new_arr);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry )
Another way to exclude duplicates What you do is use a loop to iterate through the array and use the in_array() function to check if each value already exists in the new array. If it doesn't exist, add it to the new array.
Sample code:
$arr = array("apple", "banana", "cherry", "apple", "banana"); $new_arr = array(); foreach ($arr as $value) { if (!in_array($value, $new_arr)) { $new_arr[] = $value; } } print_r($new_arr);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry )
array_filter() function can be used to delete All null values in the array. If you use a callback function, you can filter the values of the array based on specified conditions.
Sample code:
$arr = array("apple", "banana", "cherry", "", "banana"); $new_arr = array_filter($arr); print_r($new_arr);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry [4] => banana )
If you want to exclude duplicate elements, you can use the callback function to filter.
Sample code:
$arr = array("apple", "banana", "cherry", "apple", "banana"); $new_arr = array_filter($arr, function ($value, $key) use ($arr) { return array_search($value, $arr) === $key; }, ARRAY_FILTER_USE_BOTH); print_r($new_arr);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry )
The callback function here checks whether the index of the value is equal to the index in the array, and if so, the value is unique.
Another way to exclude duplicates is to use the array_keys() function to get the unique key in the array and use array_values () function gets the corresponding value.
Sample code:
$arr = array("apple", "banana", "cherry", "apple", "banana"); $new_arr = array_values(array_unique(array_keys($arr, true))); print_r($new_arr);
Output result:
Array ( [0] => 0 [1] => 1 [2] => 2 )
What is returned here is an array of keys, which can be converted into an array of values using the array_map() function.
Sample code:
$arr = array("apple", "banana", "cherry", "apple", "banana"); $new_arr = array_map(function ($key) use ($arr) { return $arr[$key]; }, array_values(array_unique(array_keys($arr, true)))); print_r($new_arr);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry )
The above are several ways to eliminate duplicates in arrays in PHP. Each method has different advantages and disadvantages. Choose the most appropriate method according to the specific application scenario.
The above is the detailed content of php array exclude duplicates. For more information, please follow other related articles on the PHP Chinese website!