Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP循环stdClass对象_Php_Arrays_Json_Object - Fatal编程技术网

PHP循环stdClass对象

PHP循环stdClass对象,php,arrays,json,object,Php,Arrays,Json,Object,我调用了一个webservice,它正在返回json数据。我使用: $response_json = json_decode ( $response ); 如果我打印($response),我会得到: stdClass Object ( [meta] => stdClass Object ( [no_of_pages] => 3 [current_page] => 1 [max_items_per_page] =

我调用了一个webservice,它正在返回json数据。我使用:

$response_json = json_decode ( $response );
如果我打印($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的对象无法转换为字符串。我也尝试过许多其他方法,但我似乎无法在循环中访问数据。提前感谢。

您可以将json_解码为关联数组

$response = json_decode ($response, true);

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

您可以将json_解码为关联数组

$response = json_decode ($response, true);

foreach($response as $orders) {
    echo $orders[0]['orderid'];
}
  • 不要在括号之间使用点
  • 跳过数组中的“数据”
  • 例如:

    foreach($response_json as $orders) {
        echo $orders[0]['orderid'];
    }
    
  • 不要在括号之间使用点
  • 跳过数组中的“数据”
  • 例如:

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

    如果我这样做了,那么可以:$response\u json=json\u decode($response,true);foreach($response_json as$orders){echo$orders.['data'].[0].[orderid'];echo$orders['data'].[0]['orderid'];我没有得到任何输出??抱歉,我更正了我的答案。它没有['data'],因为您已经在数组中循环了。请务必执行“print_r($orders);“抱歉,我不知道我在这里做错了什么……foreach($response_json as$orders){foreach($orders as$order){echo$order['orderid'];}}只输出2个带有警告的命令:通过$response_json['data']为foreach()inLoop提供的参数无效,如果我这样做了,可以:$response_json=json_decode($response,true);foreach($response_json as$orders){echo$orders.'data'].[0].'orderid'];echo$orders['data'][0]['orderid'];我没有得到任何输出??抱歉,我更正了我的答案。当你已经在数组中循环时,它没有['data']。请务必执行“print_r($orders);“抱歉,我不知道我在这里做错了什么……foreach($response_json as$orders){foreach($orders as$order){echo$order['orderid']}”仅输出2个带有警告的命令:通过$response_json['data']为foreach()inLoop提供的参数无效