Home > php教程 > php手册 > body text

php通过分类列表产生分类树数组的方法,分类列表数组

WBOY
Release: 2016-06-13 09:06:28
Original
1017 people have browsed it

php通过分类列表产生分类树数组的方法,分类列表数组

本文实例讲述了php通过分类列表产生分类树数组的方法。分享给大家供大家参考。具体分析如下:

这里$list 为分类列表数组,键为分类 ID,值为分类节点对象,pid 为父分类 ID

php代码如下:

$tree = array();
foreach ($list as $id => $row) {
 $pid = $row->pid;
 if ($pid == 0) {
  $tree[] = &$list[$id];
 } else if (isset($list[$pid])) {
  $parent = &$list[$pid];
  $parent->children[] = &$list[$id];
  // 增加 children 属性标识子分类列表
 }
}
Copy after login

希望本文所述对大家的php程序设计有所帮助。

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 Recommendations
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!