0

I have to try to access the value of "text" which is 4.9 in the JSON data below in PHP.

$json = '{"destination_addresses" : [ "Ibadan-Abeokuta Rd, Mokola, Ibadan, Nigeria" ],
 "origin_addresses" : [ "Ogunwale Crescent, Ibadan, Nigeria" ], 
 "rows" : [ 
     { "elements" : [
          { "distance" : 
            { "text" : "4.9 mi", "value" : 7819 }, 
            "duration" : { "text" : "18 mins", "value" : 1067 }, 
            "status" : "OK" }
                
                    ]
         } 
        ]
         
         , "status" : "OK" }';

1 Answers1

1

Your JSON is a little bit complex. I have a solution below that access text property from the nested JSON.

$array = json_decode( $json, true ); 
echo ($array['rows'][0] ["elements"][0]['distance']['text']);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Friday Ameh
  • 1,664
  • 1
  • 10
  • 15