使用迭代器渲染多维数组

时间:2012-09-05 08:14:28

标签: php arrays iterator spl

我有下一个多维数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Category 1
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => Sub 1
                            [parent] => 1
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => Sub 2
                            [parent] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [name] => Sub Sub 4
                                            [parent] => 4
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [id] => 2
            [name] => Category 2
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => Sub 3
                            [parent] => 2
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

我需要在页面上使用不同的缩进渲染此数组。例如:

  • 第1类
    • Sub 1
    • Sub 2
      • Sub Sub 4
  • 第2类
    • Sub 3

我知道我可以使用SPL RecursiveArrayIterator,但我不知道如何。你能帮帮我吗,如何像这样渲染数组?

2 个答案:

答案 0 :(得分:1)

你可以通过编写一个函数并递归调用它来完成这个:

<?php

function list_items($data)
{
    echo '<ul>';
    foreach ($data as $cat)
    {
        echo '<li>'.$cat['name'];

        if (count($cat['children']) > 0)
        {
            list_items($cat['children']);
        }

        echo '</li>';
    }

    echo '</ul>';
}

$array = array(
    array(
        'id' => 1,
        'name' => 'Category 1',
        'parent' => 0,
        'children' => array(
            array(
                'id' => 3,
                'name' => 'Category 3',
                'parent' => 1,
                'children' => array()
            ),
            array(
                'id' => 4,
                'name' => 'Category 4',
                'parent' => 1,
                'children' => array(                    
                    array(
                        'id' => 5,
                        'name' => 'Category 5',
                        'parent' => 4,
                        'children' => array()
                    ),
                    array(
                        'id' => 6,
                        'name' => 'Category 6',
                        'parent' => 4,
                        'children' => array()
                    )
                )
            )
        )
    ),
    array(
        'id' => 2,
        'name' => 'Category 2',
        'parent' => 0,
        'children' => array(
            array(
                'id' => 7,
                'name' => 'Category 7',
                'parent' => 2,
                'children' => array()
            )
        )
    )
);

list_items($array);

产地:

<ul>
    <li>Category 1
        <ul>
            <li>Category 3</li>
            <li>
                Category 4
                <ul>
                    <li>Category 5</li>
                    <li>Category 6</li>
                </ul>               
            </li>
        </ul>   
    </li>
    <li>
        Category 2
        <ul>
            <li>Category 7</li>
        </ul>       
    </li>
</ul>

答案 1 :(得分:1)

以下是如何实现RecursiveArrayIterator(基本上直接从PHP.net复制:http://www.php.net/manual/en/class.recursivearrayiterator.php#102574)。

说实话,我不确定ResursiveArrayIterator对@ MattHumphrey的例子有多大帮助。

<?php

$array = [
    [
        'id' => 1,
        'name' => 'Category 1',
        'parent' => 0,
        'children' => [
            [
                'id' => 3,
                'name' => 'Category 3',
                'parent' => 1,
                'children' => []
            ],
            [
                'id' => 4,
                'name' => 'Category 4',
                'parent' => 1,
                'children' => [
                    [
                        'id' => 5,
                        'name' => 'Category 5',
                        'parent' => 4,
                        'children' => []
                    ],
                    [
                        'id' => 6,
                        'name' => 'Category 6',
                        'parent' => 4,
                        'children' => []
                    ]
                ]
            ]
        ]
    ],
    [
        'id' => 2,
        'name' => 'Category 2',
        'parent' => 0,
        'children' => [
            [
                'id' => 7,
                'name' => 'Category 7',
                'parent' => 2,
                'children' => []
            ]
        ]
    ]
];


$iterator = new RecursiveArrayIterator($array);
echo "<ul>";
iterator_apply($iterator, 'traverseStructure', array($iterator));
echo "</ul>";

function traverseStructure($iterator) {
    while ( $iterator -> valid() ) {

        if ( $iterator -> hasChildren() ) {
            echo "<li>Children: <ul>";

            traverseStructure($iterator -> getChildren());

            echo "</li></ul>";

        }
        else {
            echo "<li>" . $iterator -> key() . ' : ' . $iterator -> current() . "</li>";
        }

        $iterator -> next();
    }
}