Home >Backend Development >PHP Problem >How to remove duplicate values ​​from an array in php

How to remove duplicate values ​​from an array in php

PHPz
PHPzOriginal
2023-04-27 09:04:32955browse

During development, we often encounter situations where we need to remove duplicate values ​​from an array. As a popular server-side scripting language, PHP provides a wealth of functions and methods for operating arrays. This article will introduce several commonly used methods to remove duplicate values ​​from arrays in PHP.

Method 1: array_unique()

array_unique() is one of the most commonly used methods in PHP to remove duplicate values ​​from an array. This function returns a new array containing only the unique values ​​in the input array. The following is an example of use:

$array = array("apple", "banana", "cherry", "banana", "orange", "apple");
$newArray = array_unique($array);
print_r($newArray);

The output result of the above code is:

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

As can be seen from the output result, the array_unique() function removes duplicates in the original array value and returns a new array. It should be noted that when this function removes duplicates, it determines whether there are duplicates based on the value, not the key.

Method 2: foreach loop to remove duplicates

In addition to using the array_unique() function, we can also loop through the array and remove duplicate values. The following is an example of using foreach loop to remove duplicates:

$array = array("apple", "banana", "cherry", "banana", "orange", "apple");
$newArray = array();
foreach ($array as $value) {
    if (!in_array($value, $newArray)) {
        $newArray[] = $value;
    }
}
print_r($newArray);

The output result of the above code is the same as method one. It should be noted that when using the in_array function to determine whether a value is repeated, you need to use the ! negation sign to negate it to ensure that non-repeating values ​​are added to the new in the array.

Method 3: array_flip()

array_flip()The function can exchange the keys and values ​​in the array to achieve the effect of deduplication. Before deduplication, we can use array_flip() to exchange keys and values ​​first, and then restore the array through array_flip(). The following is an example:

$array = array("apple", "banana", "cherry", "banana", "orange", "apple");
$newArray = array_flip(array_flip($array));
print_r($newArray);

The output result of the above code is the same as method one and method two. It should be noted that if there are duplicate values ​​in the original array, the subsequent values ​​will be lost after exchanging the key-value pairs, leaving only the last one. Therefore, when using array_flip(), you need to first use array_flip() to remove duplicate key values, and then use array_flip() to restore the original array. .

In short, the above are three methods commonly used in PHP to remove duplicate values ​​from arrays. Depending on actual needs, choosing different methods can improve program efficiency and make the code more concise and easy to understand.

The above is the detailed content of How to remove duplicate values ​​from 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