$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 '
)
);
The recursive conversions are as follows:
$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 '
)
),
......
);
How would you like to write this recursion?
Reply to discussion (solution)
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 ( ' 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 ' ) );p Rint_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
)
)
)
)
)