Home >Backend Development >PHP Tutorial >How to remove duplicates from php two-dimensional array

How to remove duplicates from php two-dimensional array

不言
不言Original
2018-04-17 15:16:034814browse

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
        )

)

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

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

Look at the results

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

3. Use array functions to remove duplicate values

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

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!

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
Previous article:php custom sortingNext article:php custom sorting