+ -
当前位置:首页 → 问答吧 → 怎样才能去除二维数组的并集重复值

怎样才能去除二维数组的并集重复值

时间:2011-12-18

来源:互联网

PHP code


$data = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [node_id] => 4
                    [node_pid] => 0
                )

            [1] => Array
                (
                    [node_id] => 1
                    [node_pid] => 0
                )

            [2] => Array
                (
                    [node_id] => 8
                    [node_pid] => 4
                )

            [3] => Array
                (
                    [node_id] => 3
                    [node_pid] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [node_id] => 4
                    [node_pid] => 0
                )

            [1] => Array
                (
                    [node_id] => 1
                    [node_pid] => 0
                )

            [2] => Array
                (
                    [node_id] => 9
                    [node_pid] => 4
                )

            [3] => Array
                (
                    [node_id] => 5
                    [node_pid] => 4
                )

            [4] => Array
                (
                    [node_id] => 2
                    [node_pid] => 1
                )

        )

)


$MenuArr = array();
foreach ($data as $m){
    $MenuArr = array_merge($MenuArr,$m);
}

//结果:

Array
(
    [0] => Array
        (
            [node_id] => 4
            [node_pid] => 0
        )

    [1] => Array
        (
            [node_id] => 1
            [node_pid] => 0
        )

    [2] => Array
        (
            [node_id] => 8
            [node_pid] => 4
        )

    [3] => Array
        (
            [node_id] => 3
            [node_pid] => 1
        )

    [4] => Array
        (
            [node_id] => 4
            [node_pid] => 0
        )

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

    [6] => Array
        (
            [node_id] => 9
            [node_pid] => 4
        )

    [7] => Array
        (
            [node_id] => 5
            [node_pid] => 4
        )

    [8] => Array
        (
            [node_id] => 2
            [node_pid] => 1
        )

)





[0] => Array
  (
  [node_id] => 4
  [node_pid] => 0
  )

[1] => Array
  (
  [node_id] => 1
  [node_pid] => 0
  )
并集的时候由于数字键名不同,还是重复了一次

怎样才能去掉二维数组中重复的值呢?


最近在做多角色的权限控制,如果一个用户有多个角色要合并一下菜单项
小弟分少,还请大侠帮帮忙。。。

作者: gsx31   发布时间: 2011-12-18

这还不简单。foreach一次用你想不重复的数据当key。

作者: ci1699   发布时间: 2011-12-18