Home  >  Article  >  Backend Development  >  How to remove duplicate data in an array in php

How to remove duplicate data in an array in php

PHPz
PHPzOriginal
2023-04-25 09:01:151082browse

In PHP, array is an important data type, which can conveniently store multiple data. But in actual applications, repeated data in the array may cause us some troubles and even cause program errors. Therefore, it is necessary for us to learn how to remove duplicate data from an array.

There are many ways to deduplicate PHP arrays. Below I will introduce four commonly used methods.

1. Use the array_unique function

array_unique function can remove duplicate elements in the array. The sample code is as follows:

$arr = array('a', 'b', 'c', 'a');
$arr = array_unique($arr);
print_r($arr);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

Above In the code, we first define an array with duplicate elements, then use the array_unique function to remove duplicate elements from the array, and output the result.

It should be noted that the array_unique function will retain the first element in the array by default and delete subsequent duplicate elements. If you need to retain the last occurrence of a duplicate element, you can use a combination of array_reverse and array_unique functions:

$arr = array('a', 'b', 'c', 'a');
$arr = array_reverse($arr);
$arr = array_unique($arr);
$arr = array_reverse($arr);
print_r($arr);

The output result is:

Array
(
    [0] => b
    [1] => c
    [2] => a
)

2. Use loop traversal to delete duplicate elements

We can loop through the array and use the unset function to delete duplicate elements. The sample code is as follows:

$arr = array('a', 'b', 'c', 'a');
for ($i = 0; $i < count($arr); $i++) {
    for ($j = $i + 1; $j < count($arr); $j++) {
        if ($arr[$i] == $arr[$j]) {
            unset($arr[$j]);
        }
    }
}
print_r($arr);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

In the above code, we use a two-level loop to traverse the array , first point $i to the first element of the array, and then point $j to the elements after $i. If it is found that $i and $j are equal, use the unset function to delete the $j element, and finally output the result.

3. Use the array_flip function

If the elements in the array are unique (that is, not repeated), we can use the array_flip function to exchange the keys and values, and then use the array_flip function to exchange them back. , so that duplicate elements can be removed. The sample code is as follows:

$arr = array('a', 'b', 'c');
$arr = array_flip($arr);
$arr = array_flip($arr);
print_r($arr);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

4. Using the array_reduce function

We can use the array_reduce function to compress the array into a value, which is iterable Data results. In our case, we can take each element in the array as a key with a value of 1, and then compress those keys into an array via array_reduce. The sample code is as follows:

$arr = array('a', 'b', 'c', 'a');
$arr = array_reduce($arr, function ($memo, $item) {
    $memo[$item] = 1;
    return $memo;
}, array());
$arr = array_keys($arr);
print_r($arr);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

In the above code, we use the array_reduce function to set each element in the array as a key, set the value to 1, and then use the array_keys function Convert keys to elements in an array and output the result.

Summary

There are many ways to remove duplicate data in arrays in PHP. We can choose the appropriate method according to the actual situation. Among them, the array_unique function is the most common and simple method, and it is recommended that beginners master it first.

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

Statement:
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