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
Release: 2023-04-19 09:55:51
Original
563 people have browsed it

With the development of the Internet, PHP has become one of the most popular programming languages. In PHP development, it is often necessary to use the data structure of arrays. In actual development, we often need to perform deduplication operations on arrays. This article will introduce you to how to remove duplicates from two-dimensional arrays in PHP.

A two-dimensional array is a large array composed of a bunch of arrays. If we need to deduplicate these arrays, we need to use the array_unique() function in PHP. However, if we directly use the array_unique() function to remove duplicate items in a two-dimensional array, we will not be able to achieve the desired effect.

The following is an example of a two-dimensional array in which we need to remove duplicates.

$arr = array(
    array('id'=>1, 'name'=>'Tom'),
    array('id'=>2, 'name'=>'Jerry'),
    array('id'=>3, 'name'=>'Bob'),
    array('id'=>1, 'name'=>'Tom')
);
Copy after login

If we use the array_unique() function to remove duplicates, as shown below:

$arr = array_unique($arr);
Copy after login

Then we will find that the execution result is not as we expected. We need to use the following method to correctly remove duplicates in a two-dimensional array.

Method 1:

First we convert the array that needs to be deduplicated into a string, and then use the array_unique() function to remove duplicates. Finally, convert the result into a PHP array.

$str = '';
foreach ($arr as $k => $v) {
    $str .= implode(',', $v) . ';';
}
$str = implode(',', array_unique(explode(';', trim($str, ';'))));
$arr = array();
foreach (explode(';', $str) as $k => $v) {
    $arr[$k] = explode(',', $v);
}
print_r($arr);
Copy after login

Although this method can remove duplicate items in a two-dimensional array, it is more cumbersome and not efficient.

Method 2:

We can use another function array_column() in PHP, which can extract all a field from a two-dimensional array and return a one-dimensional array .

$arr = array(
    array('id'=>1, 'name'=>'Tom'),
    array('id'=>2, 'name'=>'Jerry'),
    array('id'=>3, 'name'=>'Bob'),
    array('id'=>1, 'name'=>'Tom')
);
$idArr = array_column($arr, 'id');
$countArr = array_count_values($idArr);
foreach ($arr as $k => $v) {
    if ($countArr[$v['id']] > 1) {
        unset($arr[$k]);
    }
}
print_r($arr);
Copy after login

This method is relatively simple and only requires the use of two functions. First use the array_column() function to extract all the values ​​of the id field in the two-dimensional array, and then use the array_count_values() function to count these values. Finally, the original array is traversed. If the corresponding id count is greater than 1 in the $idArr array, it means that the item needs to be removed.

As can be seen from the above two methods, deduplication of two-dimensional arrays is slightly more troublesome than one-dimensional arrays. But if we adopt appropriate methods, we can still remove duplicates relatively easily.

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template