Problems traversing nested json

Asked

Viewed 271 times

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

1 answer

2


Every time you’re riding one array to add to variable $aux, you are assigning the value of that moment and not adding to the vector.

You’re doing it like this:

$aux = array(...);

And you should do so:

$aux[] = array(...);

Or

array_push($aux, array(...));
  • 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.

  • It may be that at last foreach you are only picking up the first item from array, 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.

  • It’s true in the json answer was just coming a result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.