0
I have a sponse in json I’m having a problem walking through it, the first is that I can only get to a part of the json and not at the value I want, the second that is conseguente of the first is not to be running through the number of sponse that it returns to me. For example it returns a list with 3 but only traverses and captures the value of a.
My Reply JSON
[
{
"geocoded_waypoints": [
{
"geocoder_status": "OK",
"place_id": "ChIJh5W-RbJzPIYRixw_andVya4",
"types": [
"establishment",
"point_of_interest"
]
},
{
"geocoder_status": "OK",
"place_id": "ChIJeRXE_uZSeYYR7KP0sO1KcE8",
"types": [
"establishment",
"food",
"meal_takeaway",
"point_of_interest",
"restaurant"
]
}
],
"routes": [
{
"bounds": {
"northeast": {
"lat": 30.1853475,
"lng": -93.3394257
},
"southwest": {
"lat": 23.7202912,
"lng": -99.1646927
}
},
"copyrights": "Map data ©2018 Google, INEGI",
"legs": [
{
"distance": {
"text": "695 mi",
"value": 1118019
},
"duration": {
"text": "11 hours 20 mins",
"value": 40783
},
"end_address": "Calz Gral Luis Caballero 732, Zozaya, 87070 Cd Victoria, Tamps., Mexico",
"end_location": {
"lat": 23.7202912,
"lng": -99.1646927
},
"start_address": "301 Main St, Hackberry, LA 70645, USA",
"start_location": {
"lat": 30.0351393,
"lng": -93.3394257
}
}
]
}
]
}
]
Note: I am receiving a list of this same answer, where I want the specific values of this list.
My code
<?php
$v = [];
$aux = [];
foreach ($json as $dest) {
foreach ($dest->routes as $key => $value) {
foreach ($value->legs[0] as $k => $val) {
$aux = array( "distancia" => $val->distance->text, "lat" => $val->end_location->lat, "lng" => $val->end_location->lng );
}
}
}
?>
Obs: the value I want is what’s in the variable $aux
, but it only gives me one of the values of the json response list
So I did as you said, but I still have the same result only a single record of a list of three, but if I go up a level of the loop it returns me the three record but I can’t get the value I want.
– William
It may be that at last
foreach
you are only picking up the first item fromarray
, try like this:foreach ($value->legs as $k => $val) {
. And in the JSON you have shown there is only one result for the information you want.– Thiago Magalhães
It’s true in the json answer was just coming a result.
– William