Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​from php two-dimensional array

How to remove duplicate values ​​from php two-dimensional array

PHPz
PHPzOriginal
2023-04-20 15:03:39571browse

In PHP, two-dimensional array is a very popular data structure. However, when we need to deduplicate data in a two-dimensional array, we may encounter some difficulties.

This article will introduce how to deduplicate two-dimensional arrays in PHP and retain unique values. We'll discuss two common practices: using a loop and using the array_map() function.

  1. Using loops

Using a loop to traverse a two-dimensional array to find duplicate values ​​and delete them is the most direct method. Here is a sample code:

function unique_arr($array2D){
    foreach ($array2D as $k=>$v){
        $tem[$k] = serialize($v);
    }
    $tem = array_unique($tem); 
    foreach ($tem as $k=>$v){
        $array[$k] = unserialize($v);
    }
    return $array;
}

In the above code, we first use a foreach loop to traverse the two-dimensional array, serialize the array and store it in a temporary array $tem. Then we use the array_unique() function to remove duplicate values ​​in $tem. Finally, we use the foreach loop again to deserialize and store it in the array $array. Ultimately, the function returns a non-duplicating two-dimensional array.

  1. Using the array_map() function

In PHP, the array_map() function can be used to execute a callback function on each element in the array. Removing duplicate values ​​from a two-dimensional array can also be easily done using the array_map() function. The code example is as follows:

function unique_arr($array2D){
    $serialize_array = array_map('serialize', $array2D);
    $unique_serialize_array = array_unique($serialize_array);
    $array = array_map('unserialize', $unique_serialize_array);
    return $array;
}

Like the first method, in this example code, we first serialize and store the array in a new array $serialize_array. Next, we use the array_unique() function to remove duplicate values ​​in $serialize_array. Finally, we use the array_map() function to deserialize the serialized array and store it in $array, ultimately returning a non-duplicate two-dimensional array.

3. Summary

In PHP, there are two common ways to remove duplicate values ​​from a two-dimensional array: using a loop and using the array_map() function. Although both methods can achieve functionality, using the array_map() function is easier to read and understand due to its simple and understandable nature.

In short, when processing two-dimensional arrays, we need to pay attention to various operations on the array in order to process our data more efficiently and conveniently.

The above is the detailed content of How to remove duplicate values ​​from php two-dimensional array. 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