类别和子类别的 PHP 树结构,无需循环查询 [英] PHP tree structure for categories and sub categories without looping a query
问题描述
我正在尝试创建一个包含任意数量子类别的类别列表,其中子类别也可以有自己的子类别.
I'm trying to create a list of categories with any number of sub categories, where sub categories can also has their own sub categories.
我已经从 Mysql 数据库中选择了所有类别,猫在一个标准的关联数组列表中,每个类别都有一个 id、名称、parentid,如果它是顶级的,那么 parentid 为 0.
I have selected all categories from the Mysql db, the cats are in a standard associate array list, each category has an id, name, parentid where the parentid is 0 if it's top level.
我基本上希望能够将猫的单级数组转换为多维数组结构,其中每个类别都可以有一个元素,该元素将包含子猫数组.
I basically want to be able to take the single level array of cats and turn it into a multidimensional array structure where each category can have an element which will contain an array of subcats.
现在,我可以通过对每个类别循环查询来轻松实现这一点,但这远非理想,我正在尝试在不影响数据库的情况下执行此操作.
Now, I can easily achieve this by looping a query for each category but this is far from ideal, I'm trying to do it without any extra hits on the db.
我知道我需要一个递归函数.任何人都可以为这种树式结构指出正确的方向吗?
I understand I need a recursive function for this. Can anyone point me in the right direction for this tree style structure?
干杯
推荐答案
这样做:
$items = array(
(object) array('id' => 42, 'parent_id' => 1),
(object) array('id' => 43, 'parent_id' => 42),
(object) array('id' => 1, 'parent_id' => 0),
);
$childs = array();
foreach($items as $item)
$childs[$item->parent_id][] = $item;
foreach($items as $item) if (isset($childs[$item->id]))
$item->childs = $childs[$item->id];
$tree = $childs[0];
print_r($tree);
这首先通过 parent_id 对类别进行索引.然后对于每个类别,我们只需要将category->childs
设置为childs[category->id]
,树就建好了!
This works by first indexing categories by parent_id. Then for each category, we just have to set category->childs
to childs[category->id]
, and the tree is built !
所以,现在 $tree
是类别树.它包含一个 parent_id=0 的项数组,这些项本身包含一个子项数组,这些子项本身......
So, now $tree
is the categories tree. It contains an array of items with parent_id=0, which themselves contain an array of their childs, which themselves ...
print_r($tree)
的输出:
stdClass Object
(
[id] => 1
[parent_id] => 0
[childs] => Array
(
[0] => stdClass Object
(
[id] => 42
[parent_id] => 1
[childs] => Array
(
[0] => stdClass Object
(
[id] => 43
[parent_id] => 42
)
)
)
)
)
所以这是最终的函数:
function buildTree($items) {
$childs = array();
foreach($items as $item)
$childs[$item->parent_id][] = $item;
foreach($items as $item) if (isset($childs[$item->id]))
$item->childs = $childs[$item->id];
return $childs[0];
}
$tree = buildTree($items);
<小时>这是带有数组的相同版本,这有点棘手,因为我们需要使用引用(但同样有效):
Here is the same version, with arrays, which is a little tricky as we need to play with references (but works equally well):
$items = array(
array('id' => 42, 'parent_id' => 1),
array('id' => 43, 'parent_id' => 42),
array('id' => 1, 'parent_id' => 0),
);
$childs = array();
foreach($items as &$item) $childs[$item['parent_id']][] = &$item;
unset($item);
foreach($items as &$item) if (isset($childs[$item['id']]))
$item['childs'] = $childs[$item['id']];
unset($item);
$tree = $childs[0];
所以最终函数的数组版本:
So the array version of the final function:
function buildTree($items) {
$childs = array();
foreach($items as &$item) $childs[(int)$item['parent_id']][] = &$item;
foreach($items as &$item) if (isset($childs[$item['id']]))
$item['childs'] = $childs[$item['id']];
return $childs[0]; // Root only.
}
$tree = buildTree($items);
这篇关于类别和子类别的 PHP 树结构,无需循环查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!