PHP树结构类别和子类别,而不循环查询 [英] PHP tree structure for categories and sub categories without looping a query

查看:117
本文介绍了PHP树结构类别和子类别,而不循环查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建的类别列表与任意数量的子类,其中的子类别也可以拥有自己的子类。

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.

基本上,我希望能够把猫的单级阵列,把它变成一个多维数组结构,其中每一类可以其中将包含subcats的数组中的元素。

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。然后对于每个类别中,我们只需要设置类别 - >孩子的童车[类别 - > 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 !

所以,现在 $树是类别树。它包含与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的输出($树)

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
                                )

                        )

                )

        )

)

因此​​,这里是最后的功能:

So here is the final function:

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[$item['parent_id']][] = &$item;
    unset($item);

    foreach($items as &$item) if (isset($childs[$item['id']]))
            $item['childs'] = $childs[$item['id']];

    return $childs[0];
}

$tree = buildTree($items);

这篇关于PHP树结构类别和子类别,而不循环查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆