Home >Backend Development >PHP Tutorial >PHP无限分类输出树状图算法代码,该如何处理

PHP无限分类输出树状图算法代码,该如何处理

WBOY
WBOYOriginal
2016-06-13 12:10:221114browse

PHP无限分类输出树状图算法代码

------解决思路----------------------

$ar = array(<br />  array(<br />    'id' => 1,<br />    'pid' => 0,<br />    'name' => '中国',<br />    'son' => array(<br />      array(<br />        'id' => 3,<br />        'pid' => 1,<br />        'name' => '北京市',<br />      ),<br />    ),<br />  ),<br />  array(<br />    'id' => 2,<br />    'pid' => 0,<br />    'name' => '日本',<br />    'son' => array(<br />      array(<br />        'id' => 4,<br />        'pid' => 2,<br />        'name' => '东京市',<br />      ),<br />    ),<br />  ),<br />);<br /><br />function tree($ar, $deep=0) {<br />  foreach($ar as $item) {<br />    printf("%s%s\n", str_repeat('——', $deep), $item['name']);<br />    if(isset($item['son'])) tree($item['son'], $deep+1);<br />  }<br />}<br /><br />tree($ar);<br />
中国<br />——北京市<br />日本<br />——东京市<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