How to remove duplicates from php two-dimensional array

不言
Release: 2023-03-24 08:40:01
Original
4778 people have browsed it

The content of this article is about the method of deduplicating two-dimensional arrays in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1. First Let’s take a look at the two-digit array that needs to be processed

Array
(
    [0] => Array
        (
            [0] => 5
            [1] => 6
        )

    [1] => Array
        (
            [0] => 5
        )

    [2] => Array
        (
            [0] => 5
            [1] => 7
        )

    [3] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 14
            [3] => 28
        )

)
Copy after login

2. Convert the two-dimensional array to a one-dimensional array ($arr is your array above)

$result = array_reduce($arr, 'array_merge', array());
Copy after login

Look at the results

Array
(
    [0] => 5
    [1] => 6
    [2] => 5
    [3] => 5
    [4] => 7
    [5] => 5
    [6] => 6
    [7] => 14
    [8] => 28
)
Copy after login

3. Use array functions to remove duplicate values

$data = array_unique($result);
Copy after login
Array
(
    [0] => 5
    [1] => 6
    [4] => 7
    [7] => 14
    [8] => 28
)
Copy after login

Complete!

If there is an easier way, please give me some advice!

Related recommendations:

PHP two-dimensional array sort array_multisort

Combining two PHP two-dimensional arrays Method

How to convert a two-dimensional array into a one-dimensional array in PHP

The above is the detailed content of How to remove duplicates from php two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!