Maison > développement back-end > tutoriel php > 请教一个简单的递归

请教一个简单的递归

WBOY
Libérer: 2016-06-23 14:10:38
original
754 Les gens l'ont consulté

$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));
Copier après la connexion
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
                                )

                        )

                )

        )

)

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal