PHP循环stdClass对象 [英] PHP Loop stdClass Object

查看:138
本文介绍了PHP循环stdClass对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我接到一个返回json数据的web服务的电话。我使用:

I have a call to a webservice which is returning json data. I use:

$response_json = json_decode ( $response );

如果我print_r($ response)我会得到:

If I print_r($response) I get this:

stdClass Object
(
    [meta] => stdClass Object
    (
        [no_of_pages] => 3
        [current_page] => 1
        [max_items_per_page] => 250
        [no_of_items] => 740
    )

[data] => Array
    (
        [0] => stdClass Object
            (
                [orderid] => 322191645
                [customer] => stdClass Object
                    (
                        [city] => FELIXSTOWE

我正在尝试遍历订单:

foreach($response_json as $orders) {
    echo $orders.['data'].[0].['orderid'];
    }

,但我不断得到:

可捕获的致命错误:stdClass类的对象无法转换为字符串。我也尝试了许多其他方法,例如bu我似乎无法循环访问数据。

Catchable fatal error: Object of class stdClass could not be converted to string. I have also tried many other ways, but I just can't seem to access the data in a loop. Thanks in advance.

推荐答案

您可以json_decode作为关联数组。

You can json_decode as associative array.

$response = json_decode ($response, true);

foreach($response as $orders) {
    echo $orders[0]['orderid'];
}

这篇关于PHP循环stdClass对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆