得到孩子的父母子女

时间:2014-03-07 06:56:53

标签: php

我设法得到了父母和孩子,但我很难找到孩子的孩子

这就是页面的样子。

  [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 个答案:

答案 0 :(得分:1)

<?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;
}

我是以非常基本的方式做到的,考虑到阵列的深度不会超过3个等级。你将期待o / p为

Array
(
    [0] => Array
        (
            [parentname] => parent0
            [childname] => test
            [childrenofchild] => child1,child2
        )

    [1] => Array
        (
            [parentname] => parent1
        )

)