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

WBOY
Release: 2016-06-13 12:10:22
Original
1110 people have browsed it

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 />
Copy after login
中国<br />——北京市<br />日本<br />——东京市<br /><br />
Copy after login

Related labels:
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