Нужен правильный ответ JSON

Мне нужен ответ 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);

-1

Решение

Вам нужно сохранить ваши элементы в некоторый массив, например $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);
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector