使用多个键从多维数组创建嵌套列表

时间:2015-01-29 11:12:40

标签: php arrays multidimensional-array

我需要从这个数组创建列表

 [records] => Array
    (
        [0] => Array
            (
                [id] => 665
                [productGroupID] => 665
                [name] => AUDIO/VIDEO VALDKOND
                [showInWebshop] => 1
                [positionNo] => 619
                [added] => 1362673935
                [lastModified] => 1364198793
                [subGroups] => Array
                    (
                        [0] => Array
                            (
                                [id] => 557
                                [productGroupID] => 557
                                [name] => VIDEO
                                [showInWebshop] => 1
                                [positionNo] => 1229
                                [added] => 1361340146
                                [lastModified] => 1362131355
                                [subGroups] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 565
                                                [productGroupID] => 565
                                                [name] => TELERID
                                                [showInWebshop] => 1
                                                [positionNo] => 642
                                                [added] => 1361340916
                                                [lastModified] => 1397208602
                                                [subGroups] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [id] => 269
                                                                [productGroupID] => 269
                                                                [name] => Teler kuni 15"
                                                                [showInWebshop] => 1
                                                                [positionNo] => 1454
                                                                [added] => 1361281340
                                                                [lastModified] => 1363594045
                                                                [subGroups] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [1] => Array
                                                            (
                                                                [id] => 500
                                                                [productGroupID] => 500
                                                                [name] => Teler 19-29"
                                                                [showInWebshop] => 1
                                                                [positionNo] => 1459
                                                                [added] => 1361284369
                                                                [lastModified] => 1379684581
                                                                [subGroups] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [2] => Array
                                                            (
                                                                [id] => 509
                                                                [productGroupID] => 509
                                                                [name] => Teler 32"
                                                                [showInWebshop] => 1
                                                                [positionNo] => 1460
                                                                [added] => 1361284370
                                                                [lastModified] => 1363594480
                                                                [subGroups] => Array
                                                                    (
                                                                    )

                                                            )



                                                    )

                                            )

如何删除productGroupID和name旁边的所有值?

我做了什么?

function list($element){
    foreach($element as $value){
        if(!is_array($value)){
            echo "<li>";
            echo $value;
        }else{
            echo "<ul>";
            r($value);
            echo "</li>";
            echo "</ul>";
        }
    }
}

echo "<ul>";
echo list($output['records']).";
echo "</ul>";

这会打印所有值。我无法弄清楚,如何只打印2个每个子阵列所需的值

1 个答案:

答案 0 :(得分:0)

您必须遍历数组数组。然后你所要做的就是打印你需要的信息,并检查是否没有空的子阵列。如果是,则再次递归调用列表函数。下面是一个工作代码示例。您仍然将$output['records']作为参数传递给此函数。

function list_elements($elements) {
    echo '<ul>';
    foreach ($elements as $element) {
        echo '<li>';
        echo $element['productGroupID'] . ':' . $element['name'];
        if (!empty($element['subGroups'])) {
            list_elements($element['subGroups']);
        }
        echo '</li>';
    }
    echo '</ul>';
}