既存の配列
$tree = array ( array ( 'ID' => 1, 'PARENT' => 0, 'NAME' => '祖父', 'CHILD' => array ( array ( 'ID' => 3, 'PARENT' => 1, 'NAME' => '叔伯' ), array ( 'ID' => 4, 'PARENT' => 1, 'NAME' => '父亲', 'CHILD' => array ( array ( 'ID' => 5, 'PARENT' => 4, 'NAME' => '儿子' ) ) ) ) ), array ( 'ID' => 2, 'PARENT' => 0, 'NAME' => '祖母' ) );
<ul> <li>祖父 <ul> <li>叔伯</li> <li>父亲 <ul> <li>儿子</li> </ul> </li> </ul> </li> <li>祖母</li></ul>
あなたは再帰的です、もちろん rree まで再帰する必要があります
あなたは再帰的です、もちろんあなたは再帰的であるべきです
$tree = array ( array ( 'ID' => 1, 'PARENT' => 0, 'NAME' => '祖父', 'CHILD' => array ( array ( 'ID' => 3, 'PARENT' => 1, 'NAME' => '叔伯' ), array ( 'ID' => 4, 'PARENT' => 1, 'NAME' => '父亲', 'CHILD' => array ( array ( 'ID' => 5, 'PARENT' => 4, 'NAME' => '儿子' ) ) ) ) ), array ( 'ID' => 2, 'PARENT' => 0, 'NAME' => '祖母' ) );function 递归($ar, $deep=0) { echo str_repeat("\t", $deep) . "<ul>\n"; foreach($ar as $item) { echo str_repeat("\t", $deep + 1) . "<li>$item[NAME]"; if(isset($item['CHILD'])) { echo "\n"; 递归($item['CHILD'], $deep+2); } echo "</li>\n"; } echo str_repeat("\t", $deep) . "</ul>\n";}递归($tree);
$tree = array ( array ( 'ID' => 1, 'PARENT' => 0, 'NAME' => '祖父', 'CHILD' => array ( array ( 'ID' => 3, 'PARENT' => 1, 'NAME' => '叔伯' ), array ( 'ID' => 4, 'PARENT' => 1, 'NAME' => '父亲', 'CHILD' => array ( array ( 'ID' => 5, 'PARENT' => 4, 'NAME' => '儿子' ) ) ) ) ), array ( 'ID' => 2, 'PARENT' => 0, 'NAME' => '祖母' ) );function 递归($ar, $deep=0) { echo str_repeat("\t", $deep) . "<ul>\n"; foreach($ar as $item) { echo str_repeat("\t", $deep + 1) . "<li>$item[NAME]"; if(isset($item['CHILD'])) { echo "\n"; 递归($item['CHILD'], $deep+2); } echo "</li>\n"; } echo str_repeat("\t", $deep) . "</ul>\n";}递归($tree);