首页 > 后端开发 > php教程 > 请教一个简单的递归

请教一个简单的递归

WBOY
发布: 2016-06-23 14:10:38
原创
754 人浏览过

$arr = array(
    array(
        'id' => 1,
        'parentid' => 0,
        'title' => 'a'
    ),
    array(
        'id' => 2,
        'parentid' => 1,
        'title' => 'a-1'
    ),
    array(
        'id' => 3,
        'parentid' => 1,
        'title' => 'a-1'
    ),
    array(
        'id' => 4,
        'parentid' => 2,
        'title' => 'a-1-1'
    )  
);

递归转换为数组如下:
$arr = array(
    array(
        'id' => 1,
        'parentid' => 0,
        'title' => 'a',
        'child' => array(
                       array(
                         'id' => 2,
                         'parentid' => 1,
                         'title' => 'a-1',
                         'child' => array(
                                    ......
                                    ) 
                       array(
                         'id' => 3,
                         'parentid' => 2,
                         'title' => 'a-2'
                       )
    ), 
    ......
);
请问要如何写这个递归呢?


回复讨论(解决方案)

function findChildren($list, $p=0){  $r = array();  foreach($list as $id=>$item){    if($item['parentid'] == $p) {      $r[$id] = $item;      if($t = findChildren($list, $item['id']) )        $r[$id]['children'][] = $t;    }  }  return $r;}        $arr = array(    array(        'id' => 1,        'parentid' => 0,        'title' => 'a'    ),    array(        'id' => 2,        'parentid' => 1,        'title' => 'a-1'    ),    array(        'id' => 3,        'parentid' => 1,        'title' => 'a-1'    ),    array(        'id' => 4,        'parentid' => 2,        'title' => 'a-1-1'    )  );print_r(findChildren($arr));
登录后复制
Array
(
    [0] => Array
        (
            [id] => 1
            [parentid] => 0
            [title] => a
            [children] => Array
                (
                    [0] => Array
                        (
                            [1] => Array
                                (
                                    [id] => 2
                                    [parentid] => 1
                                    [title] => a-1
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [3] => Array
                                                        (
                                                            [id] => 4
                                                            [parentid] => 2
                                                            [title] => a-1-1
                                                        )

                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 3
                                    [parentid] => 1
                                    [title] => a-1
                                )

                        )

                )

        )

)

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板