查找PHP递归数组

时间:2014-04-16 22:30:22

标签: php arrays cakephp

我想让每个类别下的所有孩子和子孩子都得到。我有一个数组,但我需要在其对应的子类别的列表中得到它。

这些是我想要的列表。

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

这是我得到的数组:

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
                (
                )

        )

)

我正在使用此解决方案来尝试完成此操作:

  $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 个答案:

答案 0 :(得分:1)

或许可以选择以下内容:

 function ex(&$ary, $prefix)
 {
    $retval = '';
    foreach(array_keys($ary) as $i) {
      ...
      $retval .= $prefix + $ary[$i]['StudentCategory']['name'];
      if (array_key_exists($ary[$i], 'children'))
        $retval .= ex($ary[$i]['children'], '---' . $prefix);
    }
 }

答案 1 :(得分:0)

递归解决方案可能看起来有点像:

/**
 * 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 . "\n";

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

尽管可以使用非递归算法探索和修改嵌套集算法,但这种数据结构具体是分层的,并且可能是递归构建的。因此,我们需要一个递归函数来探索它。

递归编程中一种流行的方法是传递一个级别编号,最初为零,并在调用自身时递增1。因此,当一系列调用变得更深时,它会探索级别0,级别0 + 1,级别0 + 1 + 1,依此类推。只要有更多关卡探索,这个系列就会继续。如果我们希望增加探索深度的限制,$level计数器也很有用。