【发布时间】:2016-03-12 10:35:55
【问题描述】:
我需要不同数组中的 json 响应,但下面的代码并没有给出所有结果。
while ($row=mysqli_fetch_assoc($result))
{
//$data[]=$row;
$data['id']=$row['id'];
$data['name']=$row['name'];
$data['Latitude']=$row['Latitude'];
$data['Longitude']=$row['Latitude'];
$lat_user=$row['Latitude'];
$long_user=$row['Longitude'];
$res=file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json".
"?units=imperial&origins=$lat,$long&destinations=$latuser,$longuser");
$json_res=json_decode($res);
$data['time']=$json_res->{'rows'}{0}->{'elements'}{0}->{'duration'}->{'text'};
}
echo json_encode($data,true);
【问题讨论】:
-
每次循环迭代都会覆盖
$data -
如果您将
TRUE作为第二个参数传递给[json_decode()](),它会返回一个比stdClass对象更容易处理的数组。类似$json_res=json_decode($res, TRUE); $data['time']=$json_res['rows'][0]'elements'][0]['duration']['text'];