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

Apr 23, 2023 pm 04:45 PM

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;
}
Copy after login

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;
}
Copy after login

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;
    }, []);
}
Copy after login

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)));
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24