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

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

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

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);
中国
——北京市
日本
——东京市

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