Home  >  Article  >  Backend Development  >  How to remove duplicate elements from a two-dimensional array in php

How to remove duplicate elements from a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-26 14:21:10802browse

In PHP, two-dimensional array is a very common data type. However, when we are faced with a two-dimensional array containing repeated elements, how to remove the repeated elements? This article will introduce two commonly used methods to help you deal with this problem easily.

Method 1: Use the array_unique() function

The array_unique() function is a function that comes with PHP and can remove duplicate elements from the array. However, this function only works with one-dimensional arrays. So, how do you apply this to a 2D array?

We can apply the array_unique() function to each sub-array of the two-dimensional array with the help of the array_map() function, as shown below:

// 定义一个包含重复元素的二维数组
$arr = array(
    array("name" => "张三", "age" => 20),
    array("name" => "李四", "age" => 20),
    array("name" => "张三", "age" => 25),
    array("name" => "王五", "age" => 30),
    array("name" => "李四", "age" => 25)
);

// 去除重复元素
$arr = array_map("unserialize", array_unique(array_map("serialize", $arr)));

print_r($arr);

In the above code, we first use array_map The () function applies the array_unique() function to each sub-array of the two-dimensional array, and then uses the array_map() function to deserialize it to obtain the final two-dimensional array.

Method 2: Use foreach loop

In addition to the above methods, we can also use foreach loop to process repeated elements in a two-dimensional array.

The specific method is: first define a new empty array, and then use a foreach loop to traverse each sub-array in the original array to determine whether it appears in the new array. If not, add it into a new array. The code is as follows:

// 定义一个包含重复元素的二维数组
$arr = array(
    array("name" => "张三", "age" => 20),
    array("name" => "李四", "age" => 20),
    array("name" => "张三", "age" => 25),
    array("name" => "王五", "age" => 30),
    array("name" => "李四", "age" => 25)
);

// 去除重复元素
$newArr = array();
foreach ($arr as $value) {
    if (!in_array($value, $newArr)) {
        $newArr[] = $value;
    }
}

print_r($newArr);

In the above code, we use the in_array() function to determine whether each subarray has already appeared in the new array. If not, then add it to the new array.

Summary

Duplicate elements in two-dimensional arrays are a problem we often encounter in development. This article introduces two commonly used methods: using the array_unique() function and using a foreach loop. I hope it can help everyone deal with this problem easily. No matter which method is used, it needs to be selected according to the actual situation to achieve the best results.

The above is the detailed content of How to remove duplicate elements 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