【问题标题】:How to get parent of recursive array?如何获取递归数组的父级?
【发布时间】:2017-12-15 04:19:59
【问题描述】:

我有一个这样的数组:

$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' => 'value'对:

displayRecs($navArray);

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

下面的快照可视化了结果。

我想最终得到以下数组:

$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”?

【问题讨论】:

    标签: php arrays sorting recursion parent-child


    【解决方案1】:

    这是一个工作代码:

    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 父:0 文本:1A href:1a

    id:2 父:1 文本: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

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

    【讨论】:

      【解决方案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

      【讨论】:

      • 我签入了沙盒,很棒的解决方案,效果很好。
      • @bisn apokryfox 是 SO 上的可靠海报,您可以相信他的工作。
      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 2019-05-11
      • 2021-12-17
      • 2018-07-30
      • 1970-01-01
      • 2013-01-31
      • 2022-06-14
      • 2011-05-18
      相关资源
      最近更新 更多