title: How to access json array
body: <p>I'm getting google response in Json format. when I decode and print the output it works perfectly. Like this</p>
  
  <pre><code>print_r($output);
  </code></pre>
  
  <p>But when I trying to access the single parameter of the array it's not working. </p>
  
  <pre><code>print_r($output-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat); 
  </code></pre>
  
  <p>It gives this error. </p>
  
  <blockquote>
    <p>"message": "Undefined offset: 0",
    Code:</p>
  </blockquote>
  
  <pre><code>       public function mapdata(Request $request){
          $address = $request-&gt;name;
          $formattedAddress = str_replace(' ','+',$address);
          $response = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddress.'&amp;sensor=true_or_false&amp;key=APIKEY); 
          $output = json_decode($response);
          //print_r($output);
          print_r($output-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat);
        }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>  stdClass Object
    (
   [results] =&gt; Array
      (
          [0] =&gt; stdClass Object
              (
                  [address_components] =&gt; Array
                      (
                          [0] =&gt; stdClass Object
                              (
                                  [long_name] =&gt; Melbourne
                                  [short_name] =&gt; Melbourne
                                  [types] =&gt; Array
                                      (
                                          [0] =&gt; colloquial_area
                                          [1] =&gt; locality
                                          [2] =&gt; political
                                      )
  
                              )
  
                          [1] =&gt; stdClass Object
                              (
                                  [long_name] =&gt; Victoria
                                  [short_name] =&gt; VIC
                                  [types] =&gt; Array
                                      (
                                          [0] =&gt; administrative_area_level_1
                                          [1] =&gt; political
                                      )
  
                              )
  
                          [2] =&gt; stdClass Object
                              (
                                  [long_name] =&gt; Australia
                                  [short_name] =&gt; AU
                                  [types] =&gt; Array
                                      (
                                          [0] =&gt; country
                                          [1] =&gt; political
                                      )
  
                              )
  
                      )
  
                  [formatted_address] =&gt; Melbourne VIC, Australia
                  [geometry] =&gt; stdClass Object
                      (
                          [bounds] =&gt; stdClass Object
                              (
                                  [northeast] =&gt; stdClass Object
                                      (
                                          [lat] =&gt; -37.5112737
                                          [lng] =&gt; 145.5125288
                                      )
  
                                  [southwest] =&gt; stdClass Object
                                      (
                                          [lat] =&gt; -38.4338593
                                          [lng] =&gt; 144.5937418
                                      )
  
                              )
  
                          [location] =&gt; stdClass Object
                              (
                                  [lat] =&gt; -37.8136276
                                  [lng] =&gt; 144.9630576
                              )
  
                          [location_type] =&gt; APPROXIMATE
                          [viewport] =&gt; stdClass Object
                              (
                                  [northeast] =&gt; stdClass Object
                                      (
                                          [lat] =&gt; -37.5112737
                                          [lng] =&gt; 145.5125288
                                      )
  
                                  [southwest] =&gt; stdClass Object
                                      (
                                          [lat] =&gt; -38.4338593
                                          [lng] =&gt; 144.5937418
                                      )
  
                              )
  
                      )
  
                  [place_id] =&gt; ChIJ90260rVG1moRkM2MIXVWBAQ
                  [types] =&gt; Array
                      (
                          [0] =&gt; colloquial_area
                          [1] =&gt; locality
                          [2] =&gt; political
                      )
  
              )
  
       )
  
     [status] =&gt; OK
     )
  </code></pre>