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.