1

I am faced with this challenge:

I need to display data from a hierarchical, multi-dimensional array as a nested list. I want to use recursion to do it as I do not know how many levels the list might have. This is the data I am working on (simplified):

array (size=54)
  0 => 
    object(stdClass)[786]
      public 'term_id' => string '1' (length=3)
      public 'name' => string 'All' (length=25)
      public 'parent' => string '0' (length=3)
      public 'children' => array (size=2)
            0 => 
                object(stdClass)[785]
                  public 'term_id' => string '2' (length=3)
                  public 'name' => string 'Clothes' (length=25)
                  public 'parent' => string '1' (length=3)
                  public 'children' => (size=1)
                      0 => 
                        object(stdClass)[786]
                          public 'term_id' => string '3' (length=3)
                          public 'name' => string 'Scarves' (length=25)
                          public 'parent' => string '2' (length=3)
            1 =>
                object(stdClass)[785]
                  public 'term_id' => string '4' (length=3)
                  public 'name' => string 'Gloves' (length=25)
                  public 'parent' => string '1' (length=3) 

The result should be something a nested list similar to this one:

  • All
    • Clothes
      • Scarves
    • Food
      • Vegetables

After trying many time, I've built utilized array_walk_recursive and another recursive function of my own (simplified):

function attachChildren($items)
{
    foreach($items as $item) {
        echo $item->name;
        if (is_array(($item->children))) {
            $this->attachChildren($item->children);
        }
    }
}

function buildTree($value, $key)
{
    echo $value->name;
    if (is_array($value->children)) {
        $this->attachChildren($value->children);
    }           
}

array_walk_recursive($sortedArray, 'buildTree');   

The problem with this solution is getting the formatting right. Two functions produce output and I find it difficult to arrange the items in an indented list.

What is the best way to have this array displayed as an indented list with multiple levels?

1
  • walk through array and sub array. first index element ul sub array li sub array append ul>li Commented Apr 3, 2015 at 11:54

1 Answer 1

1
function walk($array){  
    foreach ($array as $key => $value) {
        echo "<ul>";
        if(is_array($value)){
            echo "<li>{$value['name']}</li>";

            walk($value);
        }
        echo "</ul>";
    }
}

$ar = [
        ['name' => 'All', ['name' => 'Clothes', ['name' =>'Scarves']]],
        ['name' => 'Gloves']
      ];

walk($ar);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.