Home >Backend Development >PHP Problem >php remove duplicates from array

php remove duplicates from array

WBOY
WBOYOriginal
2023-05-06 10:51:07523browse

In PHP development, array deduplication is a problem that often needs to be solved. When we need to process a large amount of data, how to efficiently remove duplicates in an array becomes a very important task. In this article, we will explain how to deduplicate arrays in PHP using different methods, as well as the pros and cons of these methods.

Method 1: Use array_unique function

The array_unique function is a way to handle array deduplication in PHP's built-in function. Its function is to return a new array containing the deduplicated elements in the original array.

Give an example to illustrate:

$fruits = array("apple", "banana", "orange", "banana", "pear");
$unique_fruits = array_unique($fruits);
print_r($unique_fruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [4] => pear
)

As you can see, after using the array_unique function, the duplicates in the original array are removed. However, it should be noted that this function can only handle arrays of value types and cannot handle arrays containing complex data types.

Method 2: Use loop traversal

In PHP, using a loop to traverse an array to remove duplicates is also a common way. Its principle is to traverse each element in the array and delete duplicate elements from the array.

The following is an example of using loop traversal to remove duplicates:

$fruits = array("apple", "banana", "orange", "banana", "pear");

for ($i = 0; $i < count($fruits); $i++) {
    for ($j = $i+1; $j < count($fruits); $j++) {
        if ($fruits[$i] == $fruits[$j]) {
            array_splice($fruits, $j, 1);
            $j--;
        }
    }
}

print_r($fruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)

It can be seen that the duplication in the array has been removed using loop traversal. item. However, this method also has some disadvantages:

  1. Traversing nesting can easily reduce the execution efficiency of the code, especially when traversing a large amount of data.
  2. The operation of modifying the array will cause the rearrangement of the array, which will lead to changes in the array keys, which is not conducive to subsequent operations.

Method 3: Use array_flip and array_keys functions

The array_flip function can exchange the keys and values ​​of the array, and the array_keys function is used to obtain all the key names in the array. These two functions can easily remove duplicates from an array.

The following is an example:

$fruits = array("apple", "banana", "orange", "banana", "pear");
$unique_fruits = array_keys(array_flip($fruits));

print_r($unique_fruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)

As you can see, duplicates in the array can also be removed using this method. Compared with method two, this method is more efficient and does not modify the key-value pairs.

Method 4: Use array_reduce function

The array_reduce function is a functional programming idea, which can perform customized operations on an array and return a value.

The following is an example of using the array_reduce function to remove duplicates:

$fruits = array("apple", "banana", "orange", "banana", "pear");

$unique_fruits = array_reduce($fruits, function($result, $value) {
    if (!in_array($value, $result)) {
        $result[] = $value;
    }
    return $result;
}, array());

print_r($unique_fruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)

As you can see, the array_reduce function can also be used to easily remove duplicates in an array. Duplicates. The advantage of this method compared to method two is that it has less code and fast execution speed.

Conclusion

In PHP development, deduplicating arrays is a basic operation, and there are many different ways to implement it. By comparing four different deduplication methods, we can draw the following conclusions: The

  1. array_unique function is the simplest and most convenient way to deduplicate, but it cannot handle complex data types.
  2. Loop traversal is a common way to remove duplicates, but nested traversal will cause the execution efficiency of the code to decrease.
  3. Using array_flip and array_keys functions is more efficient in deduplication and will not modify key-value pairs.
  4. Using the array_reduce function is a concise and efficient way to remove duplicates.

According to specific project needs, you can choose the most suitable deduplication method to improve code efficiency.

The above is the detailed content of php remove duplicates from array. 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
Previous article:How to call array in phpNext article:How to call array in php