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!

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
