Home >Backend Development >PHP Problem >How to remove duplicates from php arrays (three methods)

How to remove duplicates from php arrays (three methods)

PHPz
PHPzOriginal
2023-04-20 10:13:051702browse

In PHP, array is a very common data type, which can be used to store a series of related data, such as name, age, gender, etc. But in actual development, we often encounter a problem, that is, how to remove duplicate elements in an array? This article is going to introduce some common methods to implement PHP array deduplication.

Method 1: Use array_unique() function

PHP has a built-in convenient function array_unique(), which can be used to remove duplicate elements from an array. This function can accept an array as a parameter and then return a new array. The elements in the new array are independent elements in the original array.

The following is a sample code:

<?php
$array = array("a", "b", "b", "c", "c", "c");
$new_array = array_unique($array);
print_r($new_array);
?>

The output result is as follows:

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

Method 2: Use loop traversal

Another common method is to use a loop Iterate over the original array and add unique elements to a new array.

The following is the sample code:

<?php
$array = array("a", "b", "b", "c", "c", "c");
$new_array = array();

foreach ($array as $value) {
    if (!in_array($value, $new_array)) {
        $new_array[] = $value;
    }
}

print_r($new_array);
?>

The output result is also:

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

Method 3: Use the array_flip() function

PHP’s array_flip() function You can reverse the keys and values ​​of an array and return a new array. Since the keys of the array cannot be repeated, when there are duplicate values ​​in the original array, only one will be retained in the reversed array.

Then take out the key name (the value in the original array) from the new array to get a non-duplicate array.

The following is the sample code:

<?php
$array = array("a", "b", "b", "c", "c", "c");
$new_array = array_flip($array);

print_r(array_keys($new_array));
?>

The output result is also:

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

Summary

In this article, we introduced three common Methods to achieve deduplication of PHP arrays are to use the array_unique() function, use loop traversal, and use the array_flip() function. These three methods are relatively simple, and developers can choose the appropriate method to achieve array deduplication according to the specific situation.

The above is the detailed content of How to remove duplicates from php arrays (three methods). 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