如何获取递归数组的父级?

时间:2017-07-11 08:08:23

标签: php arrays sorting recursion parent-child

我有一个这样的数组:

$navArray = array(
    array(
        'id'=>1,
        'text'=>'1A',
        'href'=>'1a',
        'childs'=>array(
            array(
                'id'=>2,
                'text'=>'1B',
                'href'=>'1b',
                'childs'=>array(
                    array(
                        'id'=>4,
                        'text'=>'4D',
                        'href'=>'4d',
                        'childs'=>array()
                    ),
                    array(
                        'id'=>5,
                        'text'=>'5E',
                        'href'=>'5e',
                        'childs'=>array(
                            array(
                                'id'=>6,
                                'text'=>'6F',
                                'href'=>'6f',
                                'childs'=>array()
                            ),
                            array(
                                'id'=>7,
                                'text'=>'7G',
                                'href'=>'7g',
                                'childs'=>array()
                            ),
                        )
                    ),
                )
            ),
            array(
                'id'=>3,
                'text'=>'3C',
                'href'=>'3c',
                'childs'=>array(
                    array(
                        'id'=>8,
                        'text'=>'8H',
                        'href'=>'8h',
                        'childs'=>array()
                    )
                )
            )
        )
    )
);

我可以遍历多维数组并返回'key'=> '价值'对:

displayRecs($navArray);

function displayRecs($navArray) {
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($navArray));
    foreach($iterator as $key => $value) {
        echo ($key . ' ' . $value . '<br>');
    }
}

下面的快照可视化结果。

enter image description here

我想最终获得以下数组:

$finalArray = array(
    array('id'=>1,'parent'=>0,'text'=>'1A','href'=>'1a'),
    array('id'=>2,'parent'=>1,'text'=>'2B','href'=>'2b'),
    array('id'=>3,'parent'=>1,'text'=>'3C','href'=>'3c'),
    array('id'=>4,'parent'=>2,'text'=>'4D','href'=>'4d'),
    array('id'=>5,'parent'=>2,'text'=>'5E','href'=>'5e'),
    array('id'=>6,'parent'=>5,'text'=>'6F','href'=>'6f'),
    array('id'=>7,'parent'=>5,'text'=>'7G','href'=>'7g'),
    array('id'=>8,'parent'=>3,'text'=>'8H','href'=>'8h'),
);

如何获取父数组的'id'?

2 个答案:

答案 0 :(得分:2)

这是一个有效的代码:

function displayRec($a, $parent = "0") {
    echo "id: {$a['id']} parent: {$parent} text: {$a['text']} href: {$a['href']}";
    echo "\n";
    if (!empty($a['childs'])) {
        foreach($a['childs'] as $child) {
            displayRec($child, $a['id']);
        }

    }
}

foreach($navArray as $a) {
    displayRec($a);
}

我刚用原始数组测试它,显示:

  

id:1 parent:0 text:1A href:1a

     

id:2 parent:1 text:1B href:1b

     

id:4 parent:2 text:4D href:4d

     

id:5 parent:2 text:5E href:5e

     

id:6 parent:5 text:6F href:6f

     

id:7 parent:5 text:7G href:7g

     

id:3 parent:1 text:3C href:3c

     

id:8 parent:3 text:8H href:8h

我希望它会有所帮助!我的(递归)函数显示结果,您可以轻松地对其进行调整以将其作为数组。

答案 1 :(得分:2)

您应该手动解决。

function getArray($array, $parent = 0) { 
    $result = [];
    foreach ($array as $member) {
        $result[] = array_intersect_key($member, [ "id" => "", "text" => "", "href" => "" ])+["parent"=>$parent];
        $result = array_merge($result, getArray((isset($member["childs"])?$member["childs"]:[]), $member["id"]));
    }
    return $result;
};

print_r(getArray($navArray));

示例http://sandbox.onlinephpfunctions.com/code/eff98096efb54db9e37da0777ab243155bfab170