3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '{ "results" : [ { "address_components" : [ { "long_name" : "P9/10", "short_name" : "P9/10", "types" : [ "premise" ] }, { "long_name" : "Pochanpur Colony", "short_name" : "Pochanpur Colony", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "long_name" : "Dwarka", "short_name" : "Dwarka", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Delhi", "short_name" : "Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] }, { "long_name" : "110075", "short_name" : "110075", "types" : [ "postal_code" ] } ], "formatted_address" : "P9/10, Pochanpur Colony, Dwarka, Delhi, 110075, India", "geometry" : { "location" : { "lat" : 28.563945, "lng" : 77.04248679999999 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 28.5652939802915, "lng" : 77.0438357802915 }, "southwest" : { "lat" : 28.5625960197085, "lng" : 77.0411378197085 } } }, "place_id" : "ChIJ0zkWBPcaDTkR8OpAXK17Qp8", "types" : [ "street_address" ] }, { "address_components" : [ { "long_name" : "Pocket 8", "short_name" : "Pocket 8", "types" : [ "establishment", "point_of_interest" ] }, { "long_name" : "Kala Ata Chakki", "short_name" : "Kala Ata Chakki", "types" : [ "route" ] }, { "long_name" : "Dhul Siras", "short_name" : "Dhul Siras", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "long_name" : "Dwarka", "short_name" : "Dwarka", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Delhi", "short_name" : "Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] }, { "long_name" : "110075", "short_name" : "110075", "types" : [ "postal_code" ] } ], "formatted_address" : "Pocket 8, Dwarka Sector 23 B, Kala Ata Chakki, Dhul Siras, Dwarka, Delhi, 110075, India", "geometry" : { "location" : { "lat" : 28.5628438, "lng" : 77.0427552 }, "location_type" : "GEOMETRIC_CENTER", "viewport" : { "northeast" : { "lat" : 28.5641927802915, "lng" : 77.04410418029151 }, "southwest" : { "lat" : 28.5614948197085, "lng" : 77.04140621970849 } } }, "place_id" : "ChIJnYkGsPcaDTkRYBzk95MrbgM", "types" : [ "establishment", "point_of_interest" ] }, { "address_components" : [ { "long_name" : "Dhul Siras", "short_name" : "Dhul Siras", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "long_name" : "Dwarka", "short_name" : "Dwarka", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Delhi", "short_name" : "Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "Dhul Siras, Dwarka, Delhi, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.5667969, "lng" : 77.04649479999999 }, "southwest" : { "lat" : 28.551814, "lng" : 77.0272779 } }, "location" : { "lat" : 28.5641729, "lng" : 77.0413344 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.5667969, "lng" : 77.04649479999999 }, "southwest" : { "lat" : 28.561422, "lng" : 77.03926439999999 } } }, "place_id" : "ChIJhSDHK2EaDTkRETqqvXgJpaY", "types" : [ "political", "sublocality", "sublocality_level_2" ] }, { "address_components" : [ { "long_name" : "Dwarka", "short_name" : "Dwarka", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "New Delhi", "short_name" : "New Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "Dwarka, New Delhi, Delhi, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.6208451, "lng" : 77.0851753 }, "southwest" : { "lat" : 28.5257751, "lng" : 77.00929099999999 } }, "location" : { "lat" : 28.5921401, "lng" : 77.04604809999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.6208451, "lng" : 77.0851753 }, "southwest" : { "lat" : 28.5257751, "lng" : 77.00929099999999 } } }, "place_id" : "ChIJ4cTuMZwaDTkRmsiky3Y5STk", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "address_components" : [ { "long_name" : "Delhi", "short_name" : "Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "Delhi, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.88284, "lng" : 77.34488 }, "southwest" : { "lat" : 28.50197, "lng" : 76.8378199 } }, "location" : { "lat" : 28.6618976, "lng" : 77.2273958 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.88284, "lng" : 77.34488 }, "southwest" : { "lat" : 28.50197, "lng" : 76.8378199 } } }, "place_id" : "ChIJL_P_CXMEDTkRw0ZdG-0GVvw", "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "110075", "short_name" : "110075", "types" : [ "postal_code" ] }, { "long_name" : "New Delhi", "short_name" : "New Delhi", "types" : [ "locality", "political" ] }, { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "New Delhi, Delhi 110075, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.6062045, "lng" : 77.07717749999999 }, "southwest" : { "lat" : 28.5547049, "lng" : 77.0103792 } }, "location" : { "lat" : 28.5899848, "lng" : 77.0443796 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.6062045, "lng" : 77.07717749999999 }, "southwest" : { "lat" : 28.5547049, "lng" : 77.0103792 } } }, "place_id" : "ChIJY690SeQaDTkRDlww_BqyZd8", "types" : [ "postal_code" ] }, { "address_components" : [ { "long_name" : "South West Delhi", "short_name" : "South West Delhi", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "South West Delhi, Delhi, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.67243, "lng" : 77.10914989999999 }, "southwest" : { "lat" : 28.50197, "lng" : 76.8378199 } }, "location" : { "lat" : 28.5928929, "lng" : 77.03461639999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.67243, "lng" : 77.10914989999999 }, "southwest" : { "lat" : 28.50197, "lng" : 76.8378199 } } }, "place_id" : "ChIJxbsO85oaDTkRGDJ4ocUr_Qw", "types" : [ "administrative_area_level_2", "political" ] }, { "address_components" : [ { "long_name" : "Delhi", "short_name" : "DL", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "Delhi, India", "geometry" : { "bounds" : { "northeast" : { "lat" : 28.881338, "lng" : 77.34845780000001 }, "southwest" : { "lat" : 28.412593, "lng" : 76.83806899999999 } }, "location" : { "lat" : 28.7040592, "lng" : 77.10249019999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 28.881338, "lng" : 77.34845780000001 }, "southwest" : { "lat" : 28.412593, "lng" : 76.83806899999999 } } }, "place_id" : "ChIJLbZ-NFv9DDkRQJY4FbcFcgM", "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "India", "short_name" : "IN", "types" : [ "country", "political" ] } ], "formatted_address" : "India", "geometry" : { "bounds" : { "northeast" : { "lat" : 35.5087008, "lng" : 97.39535869999999 }, "southwest" : { "lat" : 6.4626999, "lng" : 68.1097 } }, "location" : { "lat" : 20.593684, "lng" : 78.96288 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 35.5087008, "lng" : 97.39498069999999 }, "southwest" : { "lat" : 6.7535159, "lng" : 68.16288519999999 } } }, "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc", "types" : [ "country", "political" ] } ], "status" : "OK" }'; function getAddress($elements){ $addr = []; foreach($elements as $e){ if(isset($e['types']) && in_array("street_number",$e['types'])) $addr['street_number'] = $e['long_name']; if(isset($e['types']) && in_array("street_number",$e['types'])) $addr['route'] = $e['long_name']; if(isset($e['types']) && in_array("administrative_area_level_2",$e['types'])) $addr['city'] = $e['long_name']; } return $addr; } $obj = json_decode($json, true); $c = $obj['results'][0]['address_components']; var_dump(getAddress($c));
Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(1) { ["city"]=> string(16) "South West Delhi" }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(1) { ["city"]=> string(16) "South West Delhi" }

preferences:
157.36 ms | 402 KiB | 217 Q