How to remove duplicate data from an array in php

PHPz
Release: 2023-04-24 10:17:36
Original
3946 people have browsed it

In PHP programming, we often encounter situations where we need to remove duplicate data from an array. This article will introduce several commonly used methods to solve this problem.

Method 1: Use array_unique function

The array_unique function is PHP's built-in function, which can remove duplicate elements from an array and return a new array. This function will retain the key name, but will reset the key name, that is, the original key name will not be retained. The example is as follows:

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

Output:

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

Method 2: Use loop to remove duplicates

This method is more direct, traverse the array, and check whether the current element has appeared each time. If it does not appear before, add it to the new array, otherwise ignore it. The example is as follows:

$array = array(1, 2, 3, 3, 4, 5, 5);
$new_array = array();
foreach ($array as $value) {
    if (!in_array($value, $new_array)) {
        $new_array[] = $value;
    }
}
print_r($new_array);
Copy after login

Output:

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

Method 3: Use array_flip and array_keys functions

This method is more clever, use array_flip to exchange the keys and values ​​in the array, After removing duplicate values, use array_keys to obtain the keys in the original array. An example is as follows:

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

Output:

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

It should be noted that this method will reset the key name, and the first occurring element will be retained after removing duplicates, so the first The element's key name will become 0.

Method 4: Use the array_reduce function

This method uses the array_reduce function to traverse the array from beginning to end. Each element is checked to see if it has appeared. If it has not appeared, it is added to a new array. . An example is as follows:

$array = array(1, 2, 3, 3, 4, 5, 5);
$new_array = array_reduce($array, function($carry, $item) {
    if (!in_array($item, $carry)) {
        $carry[] = $item;
    }
    return $carry;
}, array());
print_r($new_array);
Copy after login

Output:

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

It should be noted that this method needs to implement a callback function as the second parameter. The function of this callback function is to traverse the array and return a new Array as result.

Conclusion

There are many ways to remove duplicate data from arrays in PHP, but the efficiency and readability are not the same. Choosing the most appropriate method based on the actual situation can improve the efficiency and maintainability of the code.

The above is the detailed content of How to remove duplicate data from an array in php. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template