samedi 6 juillet 2019

How to access json array

I'm getting google response in json format. when I decode and print the output it works perfectly. Like this-> print_r($output);

But when I trying to access the single parameter of the array it's not working. print_r($output->results[0]->geometry->location->lat); It gives this error. "message": "Undefined offset: 0", Code:

       public function mapdata(Request $request){
        $address = $request->name;
        $formattedAddress = str_replace(' ','+',$address);
        $response = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddress.'&sensor=true_or_false&key=AIzaSyC8retyF0d5v2jP-H6fdffd-og'); 
        $output = json_decode($response);
        //print_r($output);
        print_r($output->results[0]->geometry->location->lat);
      }

Output:

  stdClass Object
  (
 [results] => Array
    (
        [0] => stdClass Object
            (
                [address_components] => Array
                    (
                        [0] => stdClass Object
                            (
                                [long_name] => Melbourne
                                [short_name] => Melbourne
                                [types] => Array
                                    (
                                        [0] => colloquial_area
                                        [1] => locality
                                        [2] => political
                                    )

                            )

                        [1] => stdClass Object
                            (
                                [long_name] => Victoria
                                [short_name] => VIC
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [2] => stdClass Object
                            (
                                [long_name] => Australia
                                [short_name] => AU
                                [types] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [formatted_address] => Melbourne VIC, Australia
                [geometry] => stdClass Object
                    (
                        [bounds] => stdClass Object
                            (
                                [northeast] => stdClass Object
                                    (
                                        [lat] => -37.5112737
                                        [lng] => 145.5125288
                                    )

                                [southwest] => stdClass Object
                                    (
                                        [lat] => -38.4338593
                                        [lng] => 144.5937418
                                    )

                            )

                        [location] => stdClass Object
                            (
                                [lat] => -37.8136276
                                [lng] => 144.9630576
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => stdClass Object
                            (
                                [northeast] => stdClass Object
                                    (
                                        [lat] => -37.5112737
                                        [lng] => 145.5125288
                                    )

                                [southwest] => stdClass Object
                                    (
                                        [lat] => -38.4338593
                                        [lng] => 144.5937418
                                    )

                            )

                    )

                [place_id] => ChIJ90260rVG1moRkM2MIXVWBAQ
                [types] => Array
                    (
                        [0] => colloquial_area
                        [1] => locality
                        [2] => political
                    )

            )

     )

   [status] => OK
   )



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire