Home > Backend Development > PHP Problem > PHP removes duplicates from two-dimensional array

PHP removes duplicates from two-dimensional array

PHPz
Release: 2023-05-07 12:45:08
Original
2349 people have browsed it

In PHP, we often need to process and operate arrays. Sometimes, we need to remove duplicate elements in an array. Here is a deduplication method for two-dimensional arrays.

Method 1: Using array_map and serialize functions

The array_map function can apply a callback function to each element in the array, and the callback function returns a new element. We can use the serialize function to serialize each element, then use PHP's array_unique function to deduplicate the serialized elements, and finally deserialize the serialized elements into the original elements.

Sample code:

function uniqueArray($array)
{
    return array_map("unserialize", array_unique(array_map("serialize", $array)));
}

// 示例数据
$data = array(
    array("id" => 1, "name" => "apple"),
    array("id" => 2, "name" => "banana"),
    array("id" => 1, "name" => "apple"),
    array("id" => 3, "name" => "pear")
);

// 执行去重操作
$result = uniqueArray($data);

// 输出结果
print_r($result);
Copy after login

Output result:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => apple
        )
    [1] => Array
        (
            [id] => 2
            [name] => banana
        )
    [3] => Array
        (
            [id] => 3
            [name] => pear
        )
)
Copy after login
Copy after login

In the above code, we first define a uniqueArray function, which uses the array_map function to convert each element in the array Serialize, then use the array_unique function to deduplicate the serialized elements, and finally use the unserialize function to deserialize the serialized elements into the original elements.

Method 2: Use array_reduce function and array_merge function

In addition to using array_map function and serialize function, we can also use array_reduce function and array_merge function to flatten the elements in the two-dimensional array processing to form a one-dimensional array, then use the array_unique function to deduplicate the one-dimensional array, and finally use the array_map function to convert the one-dimensional array back to a two-dimensional array.

Sample code:

function uniqueArray($array)
{
    return array_map("unserialize", array_unique(array_map("serialize", array_reduce($array, "array_merge", array()))));
}

// 示例数据
$data = array(
    array("id" => 1, "name" => "apple"),
    array("id" => 2, "name" => "banana"),
    array("id" => 1, "name" => "apple"),
    array("id" => 3, "name" => "pear")
);

// 执行去重操作
$result = uniqueArray($data);

// 输出结果
print_r($result);
Copy after login

Output result:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => apple
        )
    [1] => Array
        (
            [id] => 2
            [name] => banana
        )
    [3] => Array
        (
            [id] => 3
            [name] => pear
        )
)
Copy after login
Copy after login

In the above code, we first define a uniqueArray function, which uses the array_reduce function and array_merge function to convert the two-dimensional The elements in the array are flattened to form a one-dimensional array, and then the array_map function is used to serialize, deduplicate, and deserialize the elements in the one-dimensional array, and finally the one-dimensional array is converted back to a two-dimensional array.

Summary

Both of the above two methods can perform deduplication operations on two-dimensional arrays, but method one uses serialization to retain the type of the original data during deduplication operations. , while method two requires flattening the elements in the two-dimensional array, and then converting the one-dimensional array back to a two-dimensional array. Which method to use can be chosen based on actual needs and data volume.

The above is the detailed content of PHP removes duplicates from two-dimensional array. 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