PHP用多维数组构建嵌套列表



我有一个多维数组,有许多嵌套的子级别,我想把它打印成一个格式良好的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
            )
    )

)

我把迭代器排除在外了。也许我遗漏了什么,但它似乎把一个简单的问题过于复杂了。

另外,为了实现您想要的效果,在某些时候,您必须为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

相关内容

  • 没有找到相关文章

最新更新