doushaju4901 2014-12-29 06:07
浏览 32
已采纳

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

I want to display the a;; level categories.

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;
}

I Used the above to code to display the all category level.Here I get the following output

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
                                        )

                                )

                        )

                )

        )

)

Now i want to change

 [_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
                                        )

                                )

To

 [_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
                                        )

                                )

How to i do . Please help me.

  • 写回答

2条回答 默认 最新

  • donglankui1263 2014-12-29 06:19
    关注

    If I understood your question right, You want to rename all your children's children as _subchildren. So, I am guessing that could be done by checking the value of the parent variable.
    Try replacing the if condition block in the function with this -

    if (!empty($children)) {
        if($parent == 0){
            $d['_children'] = $children;
        }else{
            $d['_subchildren'] = $children;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡