PHP循环stdClass对象

时间:2015-12-09 09:29:37

标签: php arrays json object

我调用了一个返回json数据的webservice。我用:

$response_json = json_decode ( $response );

如果我print_r($ response)我得到了这个:

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的对象无法转换为字符串。我还尝试了很多其他方法,但我似乎无法在循环中访问数据。提前谢谢。

2 个答案:

答案 0 :(得分:3)

你可以将json_decode作为关联数组。

$response = json_decode ($response, true);

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

答案 1 :(得分:1)

  1. 请勿在括号内使用点。
  2. 在您的数组中跳过“数据”。
  3. 示例:

    foreach($response_json as $orders) {
        echo $orders[0]['orderid'];
    }
    
相关问题