Escudo Bravo
Escudo Bravo

Reputation: 75

PHP Build ol li nested list by multidimensional array given

I have a multidimensional array with many nested sub levels and I want to print it as a well formatted ol li ol li ... list. So I've created this function, but it doesn't work properly:

    function loop($array) {
    echo '<ol class="dd-list">';
    $arrayObj = new ArrayObject($array);        
    foreach ( $iterator = $arrayObj->getIterator() as $key => $value ) {
        if(is_array($value)) {
            loop($iterator->current());
        } else {
            if($iterator->key()=='position') {
                echo '<li class="dd-item" data-id="' . $iterator->current() . '">';
                    echo '<div class="dd-handle">' . $iterator->key()  . '  ' . $iterator->current() . '</div>';
                echo '</li>';
            }                                           
        }
    }       
    echo '</ol>';
}   

How can I fix it? The given array is:

Array
(
[item] => Array
    (
        [0] => Array
            (
                [ID] => 22063
                [position] => 1
                [disegno] => Disegno 22063
                [items] => Array
                    (
                        [item] => Array
                            (
                                [0] => Array
                                    (
                                        [ID] => 22315
                                        [position] => 1.1
                                        [disegno] => Disegno 22315
                                    )

                                [1] => Array
                                    (
                                        [ID] => 22064
                                        [position] => 1.2
                                        [disegno] => 
                Disegno 22064

                                    )

                                [2] => Array
                                    (
                                        [ID] => 22065
                                        [position] => 1.3
                                        [disegno] => 
            Disegno 22065

                                        [items] => Array
                                            (
                                                [item] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [ID] => 22065_1
                                                                [position] => 1.3.1
                                                                [disegno] => 
                        Disegno 22065_1

                                                            )

                                                        [1] => Array
                                                            (
                                                                [ID] => 22065_2
                                                                [position] => 1.3.2
                                                                [disegno] => 
                        Disegno 22065_2

                                                            )

                                                    )

                                            )

                                    )

                                [3] => Array
                                    (
                                        [ID] => 22068
                                        [position] => 1.4
                                        [disegno] => 
            Disegno 22068

                                    )

                            )

                    )

            )

        [1] => Array
            (
                [ID] => 24728
                [position] => 2
                [disegno] => 
    Disegno 24728

            )

        [2] => Array
            (
                [ID] => 445
                [position] => 3
                [disegno] => 
    Disegno 445

            )

        [3] => Array
            (
                [ID] => 21318
                [position] => 4
                [disegno] => 
    Disegno 21318

            )

    )

)

Upvotes: 0

Views: 444

Answers (1)

Bogdan
Bogdan

Reputation: 397

I took the iterator out of the question. Maybe I'm missing something, but it seems to over-complixify a simple problem.

Also, in order to achieve what you want, at some point you would have to give the data-id attribute of the div an array, not a value. I changed that in the code below so that it receives the key - but I guess you should change that;

Here is the code:

function loop($array) {
    echo '<ol class="dd-list">';
    echo "\n";
    foreach ( $array as $key => $value ) {
        //this is the line I was talking about earlier
        echo '<li class="dd-item" data-id="' . $key . '">';
        if(is_array($value)) {
            loop($value);
        } else {
            echo '<div class="dd-handle">' . $key  . ' => ' . $value . '</div>';
        }
        echo '</li>';
        echo "\n";                                  
    }       
    echo '</ol>';
}   

$testArray = array('1', '2', array('3', '4'));
loop($testArray);

And here it is working: http://3v4l.org/ahiI9

Upvotes: 1

Related Questions