<?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'];
You have javascript disabled. You will not be able to edit any code.