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'];
}