Home >Backend Development >PHP Problem >Remove identical values ​​from php array

Remove identical values ​​from php array

WBOY
WBOYOriginal
2023-05-07 19:56:06561browse

In PHP, array is a very important data type, and it is often encountered that the same values ​​in the array need to be removed. This process is called deduplication, also known as removing duplicates. In actual development, deduplication is a necessary step to solve many problems.

PHP provides a variety of built-in functions that can be used to deduplicate arrays. This article will introduce four commonly used methods.

Method 1: Use the array_unique() function

The array_unique() function is one of the simplest and most commonly used functions in PHP. It can be used to remove duplicate values ​​from an array and return a new array.

The following is a simple example:

$arr = array(1, 2, 3, 2, 4, 3, 5);
$result = array_unique($arr);
print_r($result);

The output of this code is:

Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 4 [6] => 5 )

You can see that the duplicate values ​​2 and 3 in the original array have been was removed. However, it should be noted that the array_unique() function can only be used to remove duplicate values ​​in a one-dimensional array. If you need to remove duplicate values ​​from a multidimensional array, you need to use other methods.

Method 2: Use the array_count_values() function

array_count_values() function is used to count the number of occurrences of each value in the array and return a new array with the number of occurrences of each value as its corresponding key value.

The following is an example:

$arr = array(1, 2, 3, 2, 4, 3, 5);
$result = array_count_values($arr);
print_r($result);

The output of this code is:

Array ( [1] => 1 [2] => 2 [3] => 2 [4] => 1 [5] => 1 )

In this new array, each key represents the value that appeared in the original array , and its corresponding value represents the number of times the value appears in the original array.

If you only care about unique values ​​in the original array, you can use the array_keys() function to get all the keys in the new array to get the original number. You only need to use the array_unique() function to remove duplicate values.

Method 3: Use foreach loop

If you don’t want to use the built-in function, you can write a foreach loop to remove duplicate values ​​​​in the array.

The following is an example:

$arr = array(1, 2, 3, 2, 4, 3, 5);
$result = array();
foreach ($arr as $value) {
    if (!in_array($value, $result)) {
        $result[] = $value;
    }
}
print_r($result);

The output of this code is the same as when using the array_unique() function.

In this example, we first define an empty array $result, and then use a foreach loop to traverse all the values ​​in the original array $arr. If the current value is not in the $result array, add it to the $result array, thereby creating a new, unique array.

It is worth noting that in this loop, we use the in_array() function to determine whether the current value is in the $result array. If the value exists in the $result array, it has already been added, so we don't need to add it again.

Method 4: Use the array_flip() and array_values() functions

Finally, we will introduce a deduplication method using the array_flip() and array_values() functions.

array_flip() function can swap the keys and values ​​in an array to create a new array. If the original array contains duplicate values, this method will automatically remove the duplicate values.

The following is an example:

$arr = array(1, 2, 3, 2, 4, 3, 5);
$result = array_values(array_flip($arr));
print_r($result);

The output of this code is the same as the previous example.

In this example, we first use the array_flip() function to swap the keys and values ​​in the original array, and then use the array_values() function to reorder the values ​​in the new array. Since the original array contains duplicate values, these duplicate values ​​are automatically removed when using the array_flip() function.

Conclusion

Array deduplication is undoubtedly a very important operation in PHP. It can avoid repeated calculations during data processing and improve the running efficiency of the program. In this article, we covered several deduplication methods, including the array_unique() function, the array_count_values() function, the foreach loop, and the array_flip() and array_values() functions. They each have their own characteristics, and you can choose the method that suits your needs.

The above is the detailed content of Remove identical values ​​from php 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 loop out php arrayNext article:How to loop out php array