php二维数组去重

1、先来看一下需要处理的二位数组$arr

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、把二维数组转为一维数组

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

看一下结果

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

3、用数组函数去掉重复的值

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

完成!

如有更简便方法,请多指教!



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页