循环遍历数组并打印出数组值

时间:2011-02-07 11:24:10

标签: php arrays loops

抱歉,我没有任何可以展示的东西,但我尝试的一切都失败了。

我有一个作为对象的一部分返回的数组。我需要循环遍历值并将它们放在变量中,然后在页面上打印它们。

[Required] => Array
    (
        [0] => stdClass Object
            (
                [Field] => Field1
                [Question] => Question1
                [DataType] => Boolean
            )

        [1] => stdClass Object
            (
                [Field] => Field2
                [Question] => Question2
                [DataType] => varchar
            )

        [2] => stdClass Object
            (
                [Field] => Field3
                [Question] => Question3
                [DataType] => Boolean
            )

        [3] => stdClass Object
            (
                [Field] => Field4
                [Question] => Question5
                [DataType] => Int
            )

    )

所以例如我想打印出来

Field1是布尔值 Field2是varchar .. etc

非常感谢您的帮助

1 个答案:

答案 0 :(得分:4)

必需是一个对象属性,所以使用$ object->必需...它的值是一个数组,所以使用foreach;你想要的元素也是属性,所以

foreach($object->Required as $needed) {
    echo $needed->Field,' is ',$needed->DataType,"\n";
}