How to remove duplicate elements from an array in php

小老鼠
Release: 2023-05-25 17:19:06
original
1389 people have browsed it

php method to remove duplicate elements in an array: 1. Use the "array_unique()" function to remove duplicate data in the array; 2. Traverse through a foreach loop and define a new array to store non-duplicate data Method to achieve deduplication; 3. Use the array_flip() and array_keys() functions to get the deduplicated array; 4. Use the array_filter() function to deduplicate the original array by using this function in combination with an anonymous function.

How to remove duplicate elements from an array in php

Operating system for this tutorial: Windows 10 system, PHP version 8.1.3, Dell G3 computer

php removes duplicate elements from an array Method

Method 1: Use the array_unique() function

The array_unique() function in php can be used to remove duplicate data from the array. This function accepts an array as argument and returns a new array that does not contain duplicate values.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$arr_unique = array_unique($arr);
print_r($arr_unique);
Copy after login

Output result:

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

Method 2: Use foreach loop to traverse

In addition to using array_unique() In addition to functions, we can also achieve deduplication through loop traversal. The specific steps are as follows:

Define a new array to store unique data. Iterate over the original array, comparing each element to each element in the new array one by one, skipping if there are duplicates, and adding the element to the new array if there are no duplicates.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$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] => 1
    [1] => 2
    [3] => 3
    [4] => 4)
Copy after login
Copy after login
Copy after login

Method 3: Use array_flip() and array_keys() functions

The array_flip() function in php can be used to swap keys and values ​​in an array. By exchanging the key values ​​of the original array and then using the array_keys() function to obtain the keys, the deduplicated array can be obtained.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$new_arr = array_keys(array_flip($arr));
print_r($new_arr);
Copy after login

Output result:

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

Method 4: Use array_filter() function

array_filter in php () function can be used to filter elements in an array. We can deduplicate the original array by using this function combined with an anonymous function.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$new_arr = array_filter($arr, function ($value) {
    static $data = [];
    if (in_array($value, $data)) {
        return false;
    } else {
        $data[] = $value;
        return true;
    }
});
print_r($new_arr);
Copy after login

Output result:

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

Summary:

The methods to remove duplicate data in an array in PHP are There are many, and the methods introduced above are just a few of them. We can choose appropriate deduplication methods based on different needs and data structures to improve the efficiency and stability of the program.

The above is the detailed content of How to remove duplicate elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!