alex
alex

Reputation: 37

Find PHP Recursive array

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

Upvotes: 0

Views: 122

Answers (2)

Chrisky
Chrisky

Reputation: 567

Perhaps go for something like:

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

Upvotes: 1

halfer
halfer

Reputation: 20439

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 . "\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);
        }
    }
}

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.

Upvotes: 0

Related Questions