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

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

WBOY
Release: 2016-06-23 13:26:46
Original
865 people have browsed it

现有数组

$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' => '祖母' 		) );
Copy after login

欲达到效果的HTML代码
<ul>	<li>祖父		<ul>			<li>叔伯</li>			<li>父亲				<ul>					<li>儿子</li>				</ul>			</li>		</ul>	</li>	<li>祖母</li></ul>
Copy after login

不知道多维数组的具体维数,根据数据库得到的信息获得。

不使用递归,能实现这个效果吗


回复讨论(解决方案)

你是递归来的,当然应该递归去

$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);
Copy after login
Copy after login

你是递归来的,当然应该递归去

$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);
Copy after login
Copy after login


我这个不是通过递归获得的,是用你写的一篇文章中的方法 http://bbs.csdn.net/topics/370094009
数据原型为
$data = array(  array('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'),  array('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'),  array('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'),  array('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'),  array('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子'),);
Copy after login

不递归能演变成我欲达到的HTML代码的效果吗
<ul>    <li>祖父        <ul>            <li>叔伯</li>            <li>父亲                <ul>                    <li>儿子</li>                </ul>            </li>        </ul>    </li>    <li>祖母</li></ul>
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template