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

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

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

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

$ar = array(
array(
'id' => 1,
'pid' => 0,
'name' => '中国',
'son' => array(
array(
'id' => 3,
'pid' => 1,
'name' => '北京市',
),
),
),
array(
'id' => 2,
'pid' => 0,
'name' => '日本',
'son' => array(
array(
'id' => 4,
'pid' => 2,
'name' => '东京市',
),
),
),
);

function tree($ar, $deep=0) {
foreach($ar as $item) {
printf("%s%s\n", str_repeat('——', $deep), $item['name']);
if(isset($item['son'])) tree($item['son'], $deep+1);
}
}

tree($ar);
Copy after login
中国
——北京市
日本
——东京市

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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!