Home  >  Article  >  Backend Development  >  How to remove duplicates from sorted array in PHP

How to remove duplicates from sorted array in PHP

PHPz
PHPzOriginal
2023-04-26 10:28:09538browse

In PHP development, it is often necessary to sort arrays. However, duplicate elements may appear in the sorted array. At this time, we need to write code to delete duplicate elements from the array. This article will show you how to remove duplicates from a sorted array in PHP.

1. Use the array_unique() function

The array_unique() function can remove duplicate elements in the array. However, it should be noted that this function can only process unsorted arrays, so We need to sort the array first using functions such as sort() or rsort().

Sample code:

$arr = array(1, 2, 2, 3, 4, 4, 5);
sort($arr);//排序
$newArr = array_unique($arr);//去重
print_r($newArr);

Output result:

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

If you need to retain the key names of the original array, you can use the second parameter of the array_unique() function:

$arr = array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 4, 'g' => 5);
asort($arr);//排序
$newArr = array_unique($arr, SORT_REGULAR);//去重
print_r($newArr);

Output result:

Array ( [a] => 1 [b] => 2 [d] => 3 [e] => 4 [g] => 5 )

2. Traverse the array to delete duplicates

Another way to delete duplicates in a sorted array is to traverse the array. When duplicate elements are found, use unset () function removes the element from the array.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4, 5);
sort($arr);//排序
for($i=0;$i<count($arr)-1;$i++){
    if($arr[$i] == $arr[$i+1]){
        unset($arr[$i+1]);//删除重复元素
    }
}
print_r($arr);

Output result:

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

It should be noted that using this method to delete elements will cause the index of the original array to be reordered, so when deleting You need to use an index of $i 1, otherwise duplicate elements will be missed.

3. Use array recursion to deduplicate

If the array is multi-dimensional, we can use recursion to deduplicate.

Sample code:

$arr = array(
    1,
    2,
    2,
    array(
        3,
        4,
        4,
        5,
        array(
            6,
            7,
            7,
        ),
    ),
);
sort($arr);//排序
function uniqueArray($arr){
    foreach($arr as $k => $v){
        if(is_array($v)){
            $arr[$k] = uniqueArray($v);
        }
    }
    return array_unique($arr);
}
$newArr = uniqueArray($arr);//递归去重
print_r($newArr);

Output result:

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

This method can deduplicate multi-dimensional arrays and retain the structure of the original array.

Conclusion

In PHP development, deleting duplicates in a sorted array is a very common operation. This article introduces three methods: using the array_unique() function, traversing the array to delete, and array Recursive deduplication. Different methods are suitable for different situations, and you can choose the method that suits you according to your needs.

The above is the detailed content of How to remove duplicates from sorted 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