如何将此数组编写为php循环

时间:2010-12-15 00:29:05

标签: php

我有以下数组,如何编写循环列出名称和ID?

Array
(
[data] => Array
    (
        [0] => Array
            (
                [name] => Test name
                [id] => 110
            )

        [1] => Array
            (
                [name] => Test name 2
                [id] => 111
            )

        [2] => Array
            (
                [name] => Test name 3
                [id] => 124
            )

        [3] => Array
            (
                [name] => Test name 4
                [id] => 105
            )

        [4] => Array
            (
                [name] => Test name 5
                [id] => 56
            )

    )

感谢您的帮助:)

2 个答案:

答案 0 :(得分:4)

像这样:

foreach ($array['data'] as $key => $value) {
    echo $value['name'];
    echo $value['id'];
}

答案 1 :(得分:2)

简单的一个:

foreach($array['data'] as $item){
    echo $item['id'].'. '.$item['name'];
}