如何迭代 guzzle 响应

Leo*_*ent 2 php laravel

我从 api 请求中得到了大量响应。任何想法如何迭代响应,以便我获得持续时间文本和值。这是我的回应。我试过 foreach 但不起作用:

array:1 [?
  "elements" => array:1 [?
    0 => array:3 [?
      "distance" => array:2 [?
        "text" => "293 mi"
        "value" => 470780
      ]
      "duration" => array:2 [?
        "text" => "4 hours 50 mins"
        "value" => 17411
      ]
      "status" => "OK"
    ]
  ]
Run Code Online (Sandbox Code Playgroud)

这就是我得到这个json的方式

 $items = json_decode((string) $response->getBody(), true)['rows'][0];
        dd($items);
Run Code Online (Sandbox Code Playgroud)

T. *_*OUT 6

响应被编码为json,因此要使用它,您应该首先将其解码为 aphp-array然后解析它。

试试这个代码:

$items = json_decode((string) $response->getBody(), true)['rows'][0];
foreach ($items['elements'] as $key => $item) {
    echo $item['duration']['text'] . ': '; 
    echo $item['duration']['value'] . '<br>';   
}
Run Code Online (Sandbox Code Playgroud)