php array exclude duplicates

WBOY
Release: 2023-05-06 13:23:07
Original
399 people have browsed it

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.

  1. Using the array_unique() function

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);
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login
Copy after login
Copy after login
Copy after login
  1. Use loop traversal to exclude duplicates

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);
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login
Copy after login
Copy after login
Copy after login
  1. Using array_filter() function

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);
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => cherry [4] => banana )
Copy after login

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);
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login
Copy after login
Copy after login
Copy after login

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.

  1. Using the array_keys() and array_values() functions

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);
Copy after login

Output result:

Array ( [0] => 0 [1] => 1 [2] => 2 )
Copy after login

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);
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login
Copy after login
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!