Question

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?

Was it helpful?

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>

OTHER TIPS

Here's how to implement RecursiveArrayIterator (basically copied directly from PHP.net: http://www.php.net/manual/en/class.recursivearrayiterator.php#102574).

To be honest I'm not sure ResursiveArrayIterator helps much over @MattHumphrey's example.

<?php

$array = [
    [
        'id' => 1,
        'name' => 'Category 1',
        'parent' => 0,
        'children' => [
            [
                'id' => 3,
                'name' => 'Category 3',
                'parent' => 1,
                'children' => []
            ],
            [
                'id' => 4,
                'name' => 'Category 4',
                'parent' => 1,
                'children' => [
                    [
                        'id' => 5,
                        'name' => 'Category 5',
                        'parent' => 4,
                        'children' => []
                    ],
                    [
                        'id' => 6,
                        'name' => 'Category 6',
                        'parent' => 4,
                        'children' => []
                    ]
                ]
            ]
        ]
    ],
    [
        'id' => 2,
        'name' => 'Category 2',
        'parent' => 0,
        'children' => [
            [
                'id' => 7,
                'name' => 'Category 7',
                'parent' => 2,
                'children' => []
            ]
        ]
    ]
];


$iterator = new RecursiveArrayIterator($array);
echo "<ul>";
iterator_apply($iterator, 'traverseStructure', array($iterator));
echo "</ul>";

function traverseStructure($iterator) {
    while ( $iterator -> valid() ) {

        if ( $iterator -> hasChildren() ) {
            echo "<li>Children: <ul>";

            traverseStructure($iterator -> getChildren());

            echo "</li></ul>";

        }
        else {
            echo "<li>" . $iterator -> key() . ' : ' . $iterator -> current() . "</li>";
        }

        $iterator -> next();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top