PHP: How do I get an attribute from a JSON array?

后端 未结 3 989
-上瘾入骨i
-上瘾入骨i 2021-01-15 16:24

I have the following JSON array:



        
相关标签:
3条回答
  • 2021-01-15 16:38

    You use json_decode.

    E.g.:

    $json_obj = json_decode($json);
    echo $json_obj->results[0]->geometry->location->lat;
    

    We access the first element of the results array, then navigate through the geometry, location, and lat properties. You can also make it use associative arrays, but the default is an object.

    0 讨论(0)
  • 2021-01-15 16:41

    Greetings,

    I recommend reading the json_decode documentation

      <?php 
         $obj = json_decode($json);
         $lat = $obj->results[0]->geometry->location->lat;
      ?>
    
    0 讨论(0)
  • 2021-01-15 16:52

    You use json_decode and then $var->results[0]->geometry->location->lat;.

    json_decode yields the following structure:

    object(stdClass)[1]
      public 'status' => string 'OK' (length=2)
      public 'results' => 
        array
          0 => 
            object(stdClass)[2]
              public 'types' => 
                array
                  0 => string 'street_address' (length=14)
              public 'formatted_address' => string '5721 N Northcott Ave, Chicago, IL 60631, USA' (length=44)
              public 'address_components' => 
                array
                  0 => 
                    object(stdClass)[3]
                      public 'long_name' => string '5721' (length=4)
                      public 'short_name' => string '5721' (length=4)
                      public 'types' => 
                        array
                          0 => string 'street_number' (length=13)
                  1 => 
                    object(stdClass)[4]
                      public 'long_name' => string 'Illinois' (length=8)
                      public 'short_name' => string 'IL' (length=2)
                      public 'types' => 
                        array
                          0 => string 'administrative_area_level_1' (length=27)
                          1 => string 'political' (length=9)
                  2 => 
                    object(stdClass)[5]
                      public 'long_name' => string '60631' (length=5)
                      public 'short_name' => string '60631' (length=5)
                      public 'types' => 
                        array
                          0 => string 'postal_code' (length=11)
              public 'geometry' => 
                object(stdClass)[6]
                  public 'location' => 
                    object(stdClass)[7]
                      public 'lat' => float 41.985886
                      public 'lng' => float -87.790746
                  public 'location_type' => string 'ROOFTOP' (length=7)
                  public 'viewport' => 
                    object(stdClass)[8]
                      public 'southwest' => 
                        object(stdClass)[9]
                          public 'lat' => float 41.9827384
                          public 'lng' => float -87.7938936
                      public 'northeast' => 
                        object(stdClass)[10]
                          public 'lat' => float 41.9890336
                          public 'lng' => float -87.7875984
    
    0 讨论(0)
提交回复
热议问题