Search code examples
phparraysiteratorspl

Render multi dimensional array using Iterator


I have the next multi dimensional array:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Category 1
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => Sub 1
                            [parent] => 1
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => Sub 2
                            [parent] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [name] => Sub Sub 4
                                            [parent] => 4
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [id] => 2
            [name] => Category 2
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => Sub 3
                            [parent] => 2
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

I need to render this array on page with different indents. For example:

  • Category 1
    • Sub 1
    • Sub 2
      • Sub Sub 4
  • Category 2
    • Sub 3

I know that I can use SPL RecursiveArrayIterator for it, but I dont'know how. Can you help me with it, how to render array like this?


Solution

  • You can do this simply by writing a function and calling it recursively like so:

    <?php
    
    function list_items($data)
    {
        echo '<ul>';
        foreach ($data as $cat)
        {
            echo '<li>'.$cat['name'];
    
            if (count($cat['children']) > 0)
            {
                list_items($cat['children']);
            }
    
            echo '</li>';
        }
    
        echo '</ul>';
    }
    
    $array = array(
        array(
            'id' => 1,
            'name' => 'Category 1',
            'parent' => 0,
            'children' => array(
                array(
                    'id' => 3,
                    'name' => 'Category 3',
                    'parent' => 1,
                    'children' => array()
                ),
                array(
                    'id' => 4,
                    'name' => 'Category 4',
                    'parent' => 1,
                    'children' => array(                    
                        array(
                            'id' => 5,
                            'name' => 'Category 5',
                            'parent' => 4,
                            'children' => array()
                        ),
                        array(
                            'id' => 6,
                            'name' => 'Category 6',
                            'parent' => 4,
                            'children' => array()
                        )
                    )
                )
            )
        ),
        array(
            'id' => 2,
            'name' => 'Category 2',
            'parent' => 0,
            'children' => array(
                array(
                    'id' => 7,
                    'name' => 'Category 7',
                    'parent' => 2,
                    'children' => array()
                )
            )
        )
    );
    
    list_items($array);
    

    Produces:

    <ul>
        <li>Category 1
            <ul>
                <li>Category 3</li>
                <li>
                    Category 4
                    <ul>
                        <li>Category 5</li>
                        <li>Category 6</li>
                    </ul>               
                </li>
            </ul>   
        </li>
        <li>
            Category 2
            <ul>
                <li>Category 7</li>
            </ul>       
        </li>
    </ul>