Home >Backend Development >PHP Problem >How to remove duplicates from a two-dimensional array in PHP

How to remove duplicates from a two-dimensional array in PHP

PHPz
PHPzOriginal
2023-04-23 16:45:562829browse

When using PHP programming, two-dimensional array operations are often involved. Sometimes we need to deduplicate a two-dimensional array, that is, remove duplicate items. This article will introduce how to deduplicate two-dimensional arrays in PHP.

1. Use the array_unique function

The array_unique function can delete duplicate values ​​in the array and return a new array without duplicate values. However, the array_unique function cannot be used directly to deduplicate two-dimensional arrays.

We can use the array_walk function to traverse the array, and use the array_unique function inside each element to remove duplicates. The sample code is as follows:

function array_unique_key($arr,$key){
    $tmp_arr=array();
    foreach($arr as $k => $v){
        if(in_array($v[$key],$tmp_arr)){   //判断是否重复
            unset($arr[$k]);   //重复则删除
        }else{
            $tmp_arr[]=$v[$key];  //将值存储在临时数组中
        }
    }
    return $arr;
}

In the above code, $arr is the two-dimensional array that needs to be deduplicated, $key is the specified key name (that is, the key name corresponding to the element to be deduplicated), and the function returns The new array after deduplication.

2. Use custom functions

to implement the deduplication logic by yourself. The following example code implements deduplication using a loop:

function array_unique_key($arr,$key){
    $temp_array = array();
    foreach ($arr as $k => $v){
        if (!in_array($v[$key], $temp_array)){
            array_push($temp_array, $v[$key]);
            $temp_arr[] = $v;
        }
    }
    return $temp_arr;
}

In the above code, $arr is the two-dimensional array that needs to be deduplicated, and $key is the specified key name (that is, the element corresponding to the deduplication key name), this function returns the new array after deduplication.

3. Use the array_reduce function

The array_reduce function can iterate over each element of the array and return a reduced single value.

We can use this function to achieve deduplication of two-dimensional arrays.

The code is as follows:

function array_unique_key($arr,$key){
    return array_reduce($arr, function($carry, $item) use ($key) {
        if(!in_array($item[$key], array_column($carry, $key))) {
            $carry[] = $item;
        }
        return $carry;
    }, []);
}

In the above code, $arr is the two-dimensional array that needs to be deduplicated, and $key is the specified key name (that is, the key name corresponding to the element to be deduplicated) ), this function returns the new array after deduplication.

4. Use custom functions combined with PHP’s built-in functions

The following code implements the function of deduplicating two-dimensional arrays by combining PHP’s built-in functions array_map, array_flip and array_values. The code is as follows:

function array_unique_key($arr,$key){
   return array_values(array_flip(array_map(function($item) use($key){
        return json_encode($item[$key]);
   }, $arr)));
}

In the above code, $arr is the two-dimensional array that needs to be deduplicated, and $key is the specified key name (that is, the key name corresponding to the element to be deduplicated). This function returns the new value after deduplication. array.

Summary

The above are several methods for deduplicating two-dimensional arrays in PHP. They roughly introduce the methods of using array_unique, custom functions, and combining PHP built-in functions array_map, array_flip and array_reduce. . Different application scenarios will use different methods, and programmers can choose according to the specific situation.

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