再帰なしで多次元配列を走査する方法 (可変次元)
既存の配列
<br />$tree = array (<br /> array (<br /> 'ID' => 1,<br /> 'PARENT' => 0,<br /> 'NAME' => '祖父',<br /> 'CHILD' => array (<br /> array (<br /> 'ID' => 3,<br /> 'PARENT' => 1,<br /> 'NAME' => '叔伯' <br /> ),<br /> array (<br /> 'ID' => 4,<br /> 'PARENT' => 1,<br /> 'NAME' => '父亲',<br /> 'CHILD' => array (<br /> array (<br /> 'ID' => 5,<br /> 'PARENT' => 4,<br /> 'NAME' => '儿子' <br /> ) <br /> ) <br /> ) <br /> )<br /> <br /> ), <br /> array (<br /> 'ID' => 2,<br /> 'PARENT' => 0,<br /> 'NAME' => '祖母' <br /> ) <br />);<br />
<br /><ul><br /> <li>祖父<br /> <ul><br /> <li>叔伯</li><br /> <li>父亲<br /> <ul><br /> <li>儿子</li><br /> </ul><br /> </li><br /> </ul><br /> </li><br /> <li>祖母</li><br /></ul><br />
$tree = array (<br /> array (<br /> 'ID' => 1,<br /> 'PARENT' => 0,<br /> 'NAME' => '祖父',<br /> 'CHILD' => array (<br /> array (<br /> 'ID' => 3,<br /> 'PARENT' => 1,<br /> 'NAME' => '叔伯' <br /> ),<br /> array (<br /> 'ID' => 4,<br /> 'PARENT' => 1,<br /> 'NAME' => '父亲',<br /> 'CHILD' => array (<br /> array (<br /> 'ID' => 5,<br /> 'PARENT' => 4,<br /> 'NAME' => '儿子' <br /> ) <br /> ) <br /> ) <br /> )<br /> <br /> ), <br /> array (<br /> 'ID' => 2,<br /> 'PARENT' => 0,<br /> 'NAME' => '祖母' <br /> ) <br />);<br />function 递归($ar, $deep=0) {<br /> echo str_repeat("\t", $deep) . "<ul>\n";<br /> foreach($ar as $item) {<br /> echo str_repeat("\t", $deep + 1) . "<li>$item[NAME]";<br /> if(isset($item['CHILD'])) {<br /> echo "\n";<br /> 递归($item['CHILD'], $deep+2);<br /> }<br /> echo "</li>\n";<br /> }<br /> echo str_repeat("\t", $deep) . "</ul>\n";<br />}<br />递归($tree);<br /><br />