How PHP generates a classification tree array through a classification list
This article mainly introduces how PHP generates a classification tree array through a classification list, involving PHP operating arrays and classification nodes. Related skills have certain reference value. Friends in need can refer to them
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:
?
1
2
3
4
5
6
7
8
9
10
11
|
$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 属性标识子分类列表
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
$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];
//Add the children attribute to identify the subcategory list
}
}
|
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/987101.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987101.htmlTechArticleHow to generate a classification tree array in php through a classification list. This article mainly introduces how php generates a classification tree array through a classification list. The method involves related techniques of operating arrays and classification nodes in PHP...