如何使用php获取父项及其子项,子项类详细信息

时间:2014-12-29 06:07:48

标签: php mysqli

我想要显示a ;;等级类别。

function fn_blog_tree_format(Array $data, $parent = 0) { 
    $tree = array();
    foreach ($data as $d) {
        if ($d['parentid'] == $parent) {
            $children = fn_blog_tree_format($data, $d['id']);
            if (!empty($children)) {
                $d['_children'] = $children;

            }

            $tree[] = $d;
        }
    }


    return $tree;
}

我使用上面的代码来显示所有类别级别。这里我得到以下输出

Array
(
    [0] => Array
        (
            [id] => 1
            [categoryname] => php
            [slugurl] => core-php
            [parentid] => 0
            [description] => Lorem Ipsum is  simply dummy text of the printing and typesetting industry. Lorem Ipsum  has been the industry's standard dummy text ever since the 1500s, when  an unknown printer took a galley of type and scrambled it to make a type  specimen book. It has survived not only five centuries, but also the  leap into electronic typesetting, remaining essentially unchanged. It  was popularised in the 1960s with the release of Letraset sheets  containing Lorem Ipsum passages, and more recently with desktop  publishing software like Aldus PageMaker including versions of Lorem  Ipsum.
            [_children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [categoryname] => yii
                            [slugurl] => yii
                            [parentid] => 1
                            [description] => Contrary to popular belief, Lorem Ipsum is not simply random text. It  has roots in a piece of classical Latin literature from 45 BC, making it  over 2000 years old. Richard McClintock, a Latin professor at  Hampden-Sydney College in Virginia, looked up one of the more obscure  Latin words, consectetur, from a Lorem Ipsum passage, and going through  the cites of the word in classical literature, discovered the  undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33  of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by  Cicero, written in 45 BC. This book is a treatise on the theory of  ethics, very popular during the Renaissance. The first line of Lorem  Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section  1.10.32.
                            [_children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [categoryname] => demo
                                            [slugurl] => demo
                                            [parentid] => 2
                                            [description] => Lorem Ipsum is simply dummy text of the printing and  typesetting industry. Lorem Ipsum has been the industry's standard dummy  text ever since the 1500s, when an unknown printer took a galley of  type and scrambled it to make a type specimen book. It has survived not  only five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsu
                                        )

                                )

                        )

                )

        )

)

现在我要改变

 [_children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [categoryname] => demo
                                            [slugurl] => demo
                                            [parentid] => 2
                                            [description] => Lorem Ipsum is simply dummy text of the printing and  typesetting industry. Lorem Ipsum has been the industry's standard dummy  text ever since the 1500s, when an unknown printer took a galley of  type and scrambled it to make a type specimen book. It has survived not  only five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsu
                                        )

                                )

 [_subchildren] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [categoryname] => demo
                                            [slugurl] => demo
                                            [parentid] => 2
                                            [description] => Lorem Ipsum is simply dummy text of the printing and  typesetting industry. Lorem Ipsum has been the industry's standard dummy  text ever since the 1500s, when an unknown printer took a galley of  type and scrambled it to make a type specimen book. It has survived not  only five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsu
                                        )

                                )

我该怎么办请帮帮我。

2 个答案:

答案 0 :(得分:0)

我只是简短的想法(但没有测试过):你可以添加一个计数器。在开始时和循环中将其设置为1,在增加之前检查它是否为1。当它为1时,你会$d['_children'] = $children;(当它高于1时)$d['_subchildren'] = $children;

重新启动以重置每个父级的计数器

答案 1 :(得分:0)

如果我理解你的问题,你想把所有孩子的孩子重命名为_subchildren。 所以,我猜这可以通过检查父变量的值来完成 尝试用 -

替换函数中的if条件块
if (!empty($children)) {
    if($parent == 0){
        $d['_children'] = $children;
    }else{
        $d['_subchildren'] = $children;
    }
}