3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = ' { "type": "FeatureCollection", "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -4.702540, 51.672791 ] }, "properties": { "Id": "1603", "Name": "TENBY", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -1.878530, 50.721680 ] }, "properties": { "Id": "1603A", "Name": "Bournmouth", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -2.35, 49.433333 ] }, "properties": { "Id": "1603A", "Name": "Maseline Pier", "Country": "Channel Islands", "ContinuousHeightsAvailable": true } }] }'; $locations = json_decode($json, true); #print_r($locations); $base_location = array( 'lat' => "51.672791", 'lng' => "-4.702540" ); $distances = array(); for ($c = 0; $c < count($locations["features"]); $c++) { echo "I = " .$c; } for ($c = 0; $c < count($locations["features"]); $c++) { foreach($locations as $i){ foreach ($i as $z) { foreach ($z['geometry'] as $key => $location) { if ($key == "coordinates"){ # print_r($z); $a = $base_location['lat'] - $location[1]; $b = $base_location['lng'] - $location[0]; $distance = sqrt(($a**2) + ($b**2)); $distances[$c] = $distance; echo "The value of key '$key' is '$location'", PHP_EOL; }} }} } print_r($base_location); asort($distances); print_r($distances); $closest = $location[key($distances)]; #print_r($closest); #echo "<br>Closest port from ARRAY is: " . $closest['features'][1]['properties']['Id'];
Output for 8.1.23 - 8.1.34, 8.2.10 - 8.2.30, 8.3.0 - 8.3.29, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.2
I = 0I = 1I = 2 Warning: foreach() argument must be of type array|object, string given in /in/JX0i5 on line 69 Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: foreach() argument must be of type array|object, string given in /in/JX0i5 on line 69 Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: foreach() argument must be of type array|object, string given in /in/JX0i5 on line 69 Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Array ( [lat] => 51.672791 [lng] => -4.702540 ) Array ( [0] => 3.2480173314445 [1] => 3.2480173314445 [2] => 3.2480173314445 )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 7.3.0 - 7.3.1
I = 0I = 1I = 2 Warning: Invalid argument supplied for foreach() in /in/JX0i5 on line 69 Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Invalid argument supplied for foreach() in /in/JX0i5 on line 69 Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Warning: Invalid argument supplied for foreach() in /in/JX0i5 on line 69 Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Notice: Array to string conversion in /in/JX0i5 on line 78 The value of key 'coordinates' is 'Array' Array ( [lat] => 51.672791 [lng] => -4.702540 ) Array ( [0] => 3.2480173314445 [1] => 3.2480173314445 [2] => 3.2480173314445 )

preferences:
130.77 ms | 414 KiB | 5 Q