duansha7025 2014-03-07 06:56
浏览 25
已采纳

得到孩子的父母子女

I manage to get parent and child but I'm having trouble getting children of child

THis is how the pages looks like.

  [0] => array(1) {
    ["PARENT"] => string(0) ""
  }
  [1] => array(2) {
    ["PARENT"] => string(10) " Hang TED"
    ["CHILD"] => array(2) {
      [0] => array(1) {
        ["CHILD"] => string(13) "110379 110626"
      }
      [1] => array(1) {
        ["CHILD"] => string(13) "110417 110626"
      }
    }
  }
  • 写回答

1条回答 默认 最新

  • douluo5937 2014-03-07 09:35
    关注
    <?php
    
    $pages = array('0'=>array(
                    'parent'=> 'parent0',
                    'child' => array('name' => 'test', 'children' => array(
                            array('child' => 'child1'),
                            array('child' => 'child2')                    
                        ))
                   ),
                   '1' => array('parent'=>'parent1')
                   );
    
    
    $arr_pages = getDetails($pages);
    
    function getDetails(array $pages) {
        $result = array();
        if(count($pages)) {
            foreach($pages as $key => $value) {
                 $cnames=array();
                if($value['parent']) $result[$key]['parentname'] = $value['parent'];
                if($value['child']['name']) $result[$key]['childname'] =  $value['child']['name'];
                if(is_array($value['child']['children'])) {
                       foreach($value['child']['children'] as $cval) $cnames[] = $cval['child'];
                        $result[$key]['childrenofchild'] = implode(',',$cnames);
                }
    
            }
        }
        return $result;
    }
    

    I did it in a very basic way considering there the depth of array will be no more than 3 levels. And you will be expecting the o/p as

    Array
    (
        [0] => Array
            (
                [parentname] => parent0
                [childname] => test
                [childrenofchild] => child1,child2
            )
    
        [1] => Array
            (
                [parentname] => parent1
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题