I have a longitude and latitude as a string in PHP like below
49.648881
-103.575312
And I want to take that and look in an array of values to find the closest one. The array looks like
array(
'0'=>array('item1','otheritem1details....','55.645645','-42.5323'),
'1'=>array('item1','otheritem1details....','100.645645','-402.5323')
);
I want to return the array that has the closest long and lad. In this case it would be the first one (and yes I know -400 is not a a possible value).
Is there any quick and easy way to do this? I tried array searching but that didn't work.
Difference code
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
function distance($lat1, $long1, $lat2, $long2) { ...
? – Undershoot