php多维数组到父子关系数组

时间:2014-07-01 16:15:17

标签: php arrays parent-child relationship

我有一个像这样的多维数组:

Array
(
    [0] => Array
        (
            [id] => 14061
            [subchannel] => Array
                (
                    [0] => Array
                        (
                            [id] => 14144
                            [subchannel] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 14154
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14154name
                                        )
                                    [1] => Array
                                        (
                                            [id] => 14155
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14155name
                                        )
                                    [2] => Array
                                        (
                                            [id] => 14156
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14156name
                                        )                             
                                )
                            [name] => 14144name
                        )
                    [1] => Array
                        (
                            [id] => 14145
                            [subchannel] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 14161
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14161name
                                        )

                                    [1] => Array
                                        (
                                            [id] => 14162
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14162name
                                        )

                                    [2] => Array
                                        (
                                            [id] => 14163
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14163name
                                        )                                 
                                )
                            [name] => 14145name
                        )
                    [2] => Array
                        (
                            [id] => 14146
                            [subchannel] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 14167
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14167name
                                        )

                                    [1] => Array
                                        (
                                            [id] => 14168
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14168name
                                        )

                                    [2] => Array
                                        (
                                            [id] => 14169
                                            [subchannel] => Array
                                                (
                                                )

                                            [name] => 14169name
                                        )                                  
                                )

                            [name] => 14146name
                        )                   

                )

            [name] => 14061name
        )

    [1] => Array
        (
            [id] => 14062
            [subchannel] => Array
                (
                )

            [name] => 14062name
        )

    [2] => Array
        (
            [id] => 14063
            [subchannel] => Array
                (
                )

            [name] => 14063name
        )

)

我想将上面的多维数组更改为父子关系数组包含这个(pid表示parentid):

[id=>14061,pid=>0,name=>14061name]
[id=>14062,pid=>0,name=>14062name]
[id=>14063,pid=>0,name=>14063name]

[id=>14144,pid=>14061,name=>14144name]
[id=>14145,pid=>14061,name=>14145name]
[id=>14146,pid=>14061,name=>14146name]


[id=>14154,pid=>14144,name=>14154name]
[id=>14155,pid=>14144,name=>14155name]
[id=>14156,pid=>14144,name=>14156name]

[id=>14161,pid=>14145,name=>14161name]
[id=>14162,pid=>14145,name=>14162name]
[id=>14163,pid=>14145,name=>14163name]

[id=>14167,pid=>14146,name=>14167name]
[id=>14168,pid=>14146,name=>14168name]
[id=>14169,pid=>14146,name=>14169name]

我使用了一个函数,但只是在$ this-> data中获取ID:

function arr_foreach ($arr) 
    {           
        if (!is_array ($arr)) 
        {
            return false;
        }

        foreach ($arr as $key => $val ) 
        {
            if (is_array ($val)) 
            {
                $this->arr_foreach ($val);              
            } 
            else 
            {
                if($key == "id"){
                    $this->data[] = $val;
                }
            }
        }
    }

任何人都可以编写一个PHP代码来执行此操作吗?

1 个答案:

答案 0 :(得分:1)

你发布的尝试已经非常好了。但是,我不会递归每个数组但总是跳过一个级别。

我的意思是什么?当你进入foreach()时,你将拥有一个结构数组

[
  'id' => ..,
  'subchannel' => ..,
  'name' => ..
]

现在不是将整个数组提供给下一级递归,而是可以传递subchannel子数组。唯一剩下的就是了解父母的id。但这可以通过添加另一个参数并在递归传递当前id时轻松完成(因此它将是更深层元素的父元素)。

这将为您提供类似

的功能
function flatten($array, $parent = NULL) {
    if (!is_array($array) || empty($array))
        return;

    foreach ($array as $key=>$value) {
        //something is wrong/incomplete
        if (!is_array($value) || !isSet($value['id'], $value['subchannel'], $value['name']))
            continue;

        $this->flatten($value['subchannel'], $value['id']);
        $this->data[] = array(
            'id' => $value['id'],
            'pid' => $parent,
            'name' => $value['name']
        );
    }
}

您还会注意到我添加了条件empty($array)。由于这是在这里,在foreach()内不需要额外检查以查看元素是否实际上有子项 - 下一级递归将检查它并在必要时停止。