Home >Backend Development >PHP Tutorial >PHP method to generate classification tree array through classification list, classification list array_PHP tutorial

PHP method to generate classification tree array through classification list, classification list array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:351076browse

How PHP generates a classification tree array through a classification list, classification list array

The example in this article describes how PHP generates a classification tree array through a classification list. Share it with everyone for your reference. The specific analysis is as follows:

Here $list is a category list array, the key is the category ID, the value is the category node object, and pid is the parent category ID

The php code is as follows:

$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 属性标识子分类列表
 }
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987249.htmlTechArticleHow to generate a classification tree array in php through a classification list, a classification list array. This example describes how php generates classifications through a classification list. Tree array methods. Share it with everyone for your reference. Tool...
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