Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​from array in PHP

How to remove duplicate values ​​from array in PHP

PHPz
PHPzOriginal
2023-04-25 09:05:05423browse

PHP is a popular server-side scripting language that is widely used in the field of web development. During development, dealing with array operations is extremely common. However, in an array, duplicate values ​​may appear, and these duplicate values ​​will bring inconvenience to data processing. Therefore, in PHP, how to delete duplicate values ​​​​in an array is a problem that developers often encounter. This article will introduce how to use PHP to remove duplicate values ​​​​in an array and give code examples.

Method 1: Use array_unique()

PHP provides a built-in function array_unique(), which can help us quickly delete duplicate values ​​​​in the array. Using this function is very simple, just pass an array parameter. This function returns a new array containing all unique elements in the original array.

The following is a code example:

$my_array = array("apple", "orange","banana","apple","grape","orange");
$my_array = array_unique($my_array);
print_r($my_array);

The above code will output the following results:

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

As shown above, using the array_unique() function can remove duplicate elements in the original array , the returned results form a new unique array. But it should be noted that the array_unique() function will not rearrange the key names in the array. If you want to rearrange the key names, you need to use the array_values() function.

Method 2: Loop through deletion

Another method is to manually loop through the array to determine whether there are duplicate values, and then delete the duplicate values. The following is a code example:

$my_array = array(3, 2, 3, 4, 1, 1, 5, 6, 7, 7);

foreach($my_array as $key => $value) {
    $count = 0;
    foreach($my_array as $key2 => $value2) {
        if($value == $value2) {
            $count++;
        }
        if($count > 1) {
            unset($my_array[$key]);
        }        
    }
}
print_r($my_array);

The above code will output the following results:

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

As shown above, using the loop traversal method, you can manually delete duplicate elements by judging whether the elements are duplicated. However, this method is less efficient and is recommended only when the array_unique() function cannot be used.

Summary

In PHP, deleting duplicate values ​​in an array is a common operation. This article introduces two methods: using the array_unique() function and manual traversal deletion for readers' reference. We also need to note that when deleting array elements, you can use the unset() function, which is used to destroy the specified variable.

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