PHP通过给定的多维数组构建ol li嵌套列表

时间:2014-05-20 09:04:44

标签: php jquery arrays multidimensional-array iteration

我有一个包含许多嵌套子级别的多维数组,我想将其打印为格式良好的ol li ol li ...列表。所以我创建了这个函数,但它无法正常工作:

    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>';
}   

我该如何解决? 给定的数组是:

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

            )

    )

1 个答案:

答案 0 :(得分:1)

我没有把迭代器拿出来。也许我错过了一些东西,但它似乎过度复杂化了一个简单的问题。

另外,为了达到你想要的效果,在某些时候你必须给div的data-id属性一个数组,而不是一个值。我在下面的代码中更改了它,以便它接收密钥 - 但我想你应该改变它;

以下是代码:

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);

这里有效:http://3v4l.org/ahiI9