douxun2023 2014-04-16 22:30
浏览 46

查找PHP递归数组

I am trying to get all the children and sub-children under each category. I have an array but I need to get this in list of category with its correspond child.

These are the given list I want.

-Category a
  ---category child b
    -------category child c
  ---category d
-category b

This is an array I got return:

Array
(
    [0] => Array
        (
            [StudentCategory] => Array
                (
                    [id] => 1
                    [name] => Category A
                    [parent_id] => 0
                    [lft] => 1
                    [rght] => 8
                    [user_id] => 1
                    [description] => Category A
                    [is_active] => 1
                    [is_deleted] => 0
                    [created] => 2014-04-16 19:43:01
                    [updated] => 2014-04-17 02:27:28
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [StudentCategory] => Array
                                (
                                    [id] => 2
                                    [name] => Category A
                                    [parent_id] => 1
                                    [lft] => 2
                                    [rght] => 5
                                    [user_id] => 1
                                    [description] => Category A
                                    [is_active] => 1
                                    [is_deleted] => 0
                                    [created] => 2014-04-16 19:44:43
                                    [updated] => 2014-04-17 01:15:39
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [StudentCategory] => Array
                                                (
                                                    [id] => 3
                                                    [name] => Category A
                                                    [parent_id] => 2
                                                    [lft] => 3
                                                    [rght] => 4
                                                    [user_id] => 1
                                                    [description] => Category A
                                                    [is_active] => 1
                                                    [is_deleted] => 0
                                                    [created] => 2014-04-16 19:45:39
                                                    [updated] => 2014-04-16 19:45:39
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [StudentCategory] => Array
                                (
                                    [id] => 4
                                    [name] => category 2
                                    [parent_id] => 1
                                    [lft] => 6
                                    [rght] => 7
                                    [user_id] => 1
                                    [description] => category 2
                                    [is_active] => 0
                                    [is_deleted] => 0
                                    [created] => 2014-04-16 20:57:28
                                    [updated] => 2014-04-16 20:57:28
                                )

                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [StudentCategory] => Array
                (
                    [id] => 5
                    [name] => category 21
                    [parent_id] => 0
                    [lft] => 9
                    [rght] => 10
                    [user_id] => 1
                    [description] => category 21
                    [is_active] => 1
                    [is_deleted] => 0
                    [created] => 2014-04-16 21:00:33
                    [updated] => 2014-04-16 21:00:33
                )

            [children] => Array
                (
                )

        )

)

I am using this solution for trying to get this done:

  $level = 0;
  $catresult = array();
  $ch = $this->Common->make_category_children_array($v['children'],$level,$catresult);
  pr($ch);  

function make_category_children_array($childarray,&$level,&$catresult){

        foreach($childarray as $item){

            $catresult[$level] = $item['StudentCategory'];
            $childarray = $item['children'];
            if (is_array($childarray) && $childarray) { $level++;
                $this->make_category_children_array($childarray,$level,$catresult);
            }
        }

        return $catresult;
    }
  • 写回答

2条回答 默认 最新

  • dpowt82802 2014-04-16 22:43
    关注

    A recursive solution might look a little bit like the following:

    /**
     * An initial function to set up the recursive loop
     */
    function explore(array $structure)
    {
        exploreLevel($structure, 0);
    }
    
    /**
     * The actual recursion happens in here
     */
    function exploreLevel(array $subStructure, $level)
    {
        foreach ($subStructure as $item)
        {
            // Let's render the category here. The repeated spaces give us
            // some indentation depending where we are in the hierarchy
            $category = $item['StudentCategory'];
            echo str_repeat('  ', $level) . $category . "
    ";
    
            // Only recurse if the key is an array and it is not empty
            $children = $item['children'];
            if (is_array($children) && $children)
            {
                exploreLevel($children, $level + 1);
            }
        }
    }
    

    Although the nested set algorithm can be explored and modified using non-recursive algorithms, this data structure is specifically hierarchical, and was probably built recursively. Hence, we need a recursive function to explore it.

    A popular approach in recursive programming is to pass a level number, initially zero, and incrementing by one as it calls itself. Thus, as the series of calls gets deeper, it explores level 0, level 0 + 1, level 0 + 1 + 1, and so on. This series continues for as long as there are more levels to explore. The $level counter is also useful if we wish to add a limit to the depth of exploration.

    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM