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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)