Created
June 27, 2018 12:45
-
-
Save robozavri/f38ddb7b04f4ef27291075f38486c0af to your computer and use it in GitHub Desktop.
php Search or sort multi dimensional array for closest lat and lon values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arrGeoData = array( | |
array( | |
"zipcode" => 777, | |
"latitude" => 37.234982, | |
"longitude" => -82.913799, | |
"cityname" => "tiflis", | |
), | |
array( | |
"zipcode" => 666, | |
"latitude" => 36.664982, | |
"longitude" => -80.913799, | |
"cityname" => "gori", | |
), | |
array( | |
"zipcode" => 6366, | |
"latitude" => 49.664982, | |
"longitude" => -70.913799, | |
"cityname" => "kaspi", | |
), | |
array( | |
"zipcode" => 666, | |
"latitude" => 43.664982, | |
"longitude" => -76.913799, | |
"cityname" => "oni", | |
), | |
array( | |
"zipcode" => 2824599, | |
"latitude" => 60.234982, | |
"longitude" => -72.913799, | |
"cityname" => "ACME", | |
), | |
array( | |
"zipcode" => 17198, | |
"latitude" => 51.735880, | |
"longitude" => -73.143855, | |
"cityname" => "zeland", | |
), | |
array( | |
"zipcode" => 17198, | |
"latitude" => 54.735880, | |
"longitude" => -74.143855, | |
"cityname" => "NEWLAND", | |
), | |
array( | |
"zipcode" => 12203, | |
"latitude" => 41.711931, | |
"longitude" => -75.011806, | |
"cityname" => "ACE", | |
), | |
); | |
$nearestLongLat = sortByNearestLatLong($arrGeoData, 40.6000, -74.15000,false); | |
echo '<pre>'; | |
var_dump($nearestLongLat); | |
function sortByNearestLatLong($geoData, $lat, $long, $returnNearestOnly=true){ | |
// CREATE AN ARRAY FOR USE INSIDE THE FUNCTION | |
$arrCloseMatchLat = array(); | |
$arrCloseMatchLong = array(); | |
$matchedGeoSet = array(); | |
// LOOP THROUGH ALL THE $geoData ARRAY AND SUBTRACT THE GIVEN LAT & LONG VALUES | |
// FROM THOSE CONTAINED IN THE ORIGINAL ARRAY: $geoData | |
// WE KNOW THAT THE SMALLER THE RESULT OF THE SUBTRACTION; THE CLOSER WE ARE | |
// WE DO THIS FOR BOTH THE LONGITUDE & LATITUDE... HENCE OUR ARRAY: | |
// $arrCloseMatchLat AND $arrCloseMatchLong RESPECTIVELY | |
foreach($geoData as $iKey=>$arrGeoStrip){ | |
$arrCloseMatchLat[$iKey] = abs(floatval( ($arrGeoStrip['latitude']) - $lat )); | |
$arrCloseMatchLong[$iKey] = abs(floatval( ($arrGeoStrip['longitude']) - $long )); | |
} | |
// WE SORT BOTH ARRAYS NUMERICALLY KEEPING THE KEYS WHICH WE NEED FOR OUR FINAL RESULT | |
asort($arrCloseMatchLat, SORT_NUMERIC); | |
asort($arrCloseMatchLong, SORT_NUMERIC); | |
// WE CAN RETURN ONLY THE RESULT OF THE FIRST, CLOSEST MATCH | |
if($returnNearestOnly){ | |
foreach($arrCloseMatchLat as $index=>$difference){ | |
$matchedGeoSet['latitudes'][] = $geoData[$index]; | |
break; | |
} | |
foreach($arrCloseMatchLong as $index=>$difference){ | |
$matchedGeoSet['longitudes'][] = $geoData[$index]; | |
break; | |
} | |
// OR WE CAN RETURN THE ENTIRE $geoData ARRAY ONLY SORTED IN A "CLOSEST FIRST" FASHION... | |
// WE DO THIS FOR BOTH THE LONGITUDE & LATITUDE RESPECTIVELY SO WE END UP HAVING 2 | |
// ARRAYS: ONE THAT SORTS THE CLOSEST IN TERMS OF LONG VALUES | |
// AN ONE THAT SORTS THE CLOSEST IN TERMS OF LAT VALUES... | |
}else{ | |
foreach($arrCloseMatchLat as $index=>$difference){ | |
$matchedGeoSet['latitudes'][] = $geoData[$index]; | |
} | |
foreach($arrCloseMatchLong as $index=>$difference){ | |
$matchedGeoSet['longitudes'][] = $geoData[$index]; | |
} | |
} | |
return $matchedGeoSet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment