Home >Backend Development >PHP Tutorial >How to generate a classification tree array in PHP through a classification list_PHP tutorial

How to generate a classification tree array in PHP through a classification list_PHP tutorial

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

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

45 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...
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