Skip to content
Advertisement

Convert PHP multidimentional array to multilevel menu using recursive?

I’m trying to create a PHP recursive function to create a multilevel menu from multi-dimentional array, but still failed.

I have a data as such:

<?php
$json = '{"id":1,"name":"john","member":[{"id":9,"name":"jane","member":[]},{"id":10,"name":"lorem","member":[{"id":12,"name":"ipsum","member":[{"id":99,"name":"dolor","member":[]},{"id":109,"name":"sit","member":[{"id":999,"name":"rose","member":[]}]}]}]}]}';

$raw = json_decode($json, true);
?>

So now I have a multi-dimentional array:

(
    [id] => 1
    [name] => john
    [member] => Array
        (
            [0] => Array
                (
                    [id] => 9
                    [name] => jane
                    [member] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [id] => 10
                    [name] => lorem
                    [member] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 12
                                    [name] => ipsum
                                    [member] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 99
                                                    [name] => dolor
                                                    [member] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [id] => 109
                                                    [name] => sit
                                                    [member] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [id] => 999
                                                                    [name] => rose
                                                                    [member] => Array
                                                                        (
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

My question is, how to produce final data as below:

<ul>
        <li>john
            <ul>
                <li>jane</li>
                <li>lorem
                    <ul>
                        <li>ipsum
                            <ul>
                                <li>dolor</li>
                                <li>sit
                                    <ul>
                                        <li>rose</li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

using PHP recursive function?

I’ve been looking into several examples, but unfortunately the given solution and data sample/condition were quite different.

Advertisement

Answer

<?php

$json = '{"id":1,"name":"john","member":[{"id":9,"name":"jane","member":[]},{"id":10,"name":"lorem","member":[{"id":12,"name":"ipsum","member":[{"id":99,"name":"dolor","member":[]},{"id":109,"name":"sit","member":[{"id":999,"name":"rose","member":[]}]}]}]}]}';

$raw = json_decode($json, true);
//var_dump($raw);

function outputSubmenu($items) {
    echo '<ul>';
    foreach ($items as $data) {
        echo '<li>';
        echo $data['name'];
        if (count($data['member'])) {
            outputSubmenu($data['member']);
        }
        echo '</li>';
    }
    echo '</ul>';
}

echo outputSubmenu([$raw]);

https://3v4l.org/Gnvai

Output of the function

Note, how I had to put the $raw variable into an array, so that the first foreach works.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement