Problems while navigating json nested

0

I have a response in json I have a problem with going through it, the first is that I can only get a part of json and not the value I want , the second thing that I get from the first one is not going through the response number it gives me back. For example it returns a list with 3 responses but only traverses and captures the value of one.

My Response 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'm getting a list of this same answer, where I want the specific values from 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 );
                }
            }
        }
?>

The value I want is what is in the variable $aux , however I can even get it but it only returns one of the values of json response list

    
asked by anonymous 03.08.2018 / 18:15

1 answer

2

Each time you are mounting a array to add to the 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 it should do like this:

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

Or

array_push($aux, array(...));
    
03.08.2018 / 19:10