簡體   English   中英

需要適當的 json 響應

[英]Need proper json response

我需要不同數組中的 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);

您需要將您的項目保存到某個數組中,例如$dataArray在每次迭代結束時和循環后使用此數組創建 json

$dataArray = array();
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'};
    $dataArray[] = $data;

}
echo json_encode($dataArray,true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM