0

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

            )

    )

)

4
  • It builds "ol" for each "li", which is wrong, because I'd like to have "ol" for nested "li" groups and not singli "li". Commented May 20, 2014 at 9:11
  • Please explain more. What is the problem? Commented May 20, 2014 at 9:20
  • I want as result a list as this: ol li ol li li li /ol /li /ol when I'm wrongly having something like this: ol li /ol ol li /ol. Am I clear enough? Commented May 20, 2014 at 9:33
  • Did you end up finding your answer? Commented May 22, 2014 at 12:43

1 Answer 1

1

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

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.