Home >Backend Development >PHP Tutorial >不递归怎么遍历多维数组(维数不定)

不递归怎么遍历多维数组(维数不定)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:24:171173browse

不递归如何遍历多维数组(维数不定)
现有数组

<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 />

欲达到效果的HTML代码
<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 />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn