example:
$arr =Array(
Array("name1","lat1","lng1"),
Array("name2","lat2","lng2"),
Array("name3","lat3","lng3"),
Array("name4","lat4","lng4"),
Array("name5","lat5","lng5"),
) ;
I have a function that will return miles apart from 2 different locations
function is_within_x_miles($lat1, $lng1, $lat2, $lng2) {
return $milesApart;
}
the idea is to return an array that makes sure every location is x miles apart from each other
Actual runnable code:
<?php
$arr =Array(Array(" exclusive-care-2"," 38.677892"," -121.31073"),
Array(" the-re-up-citrus-heights-orangevale"," 38.689974"," -121.297324"),
Array(" norcal-medicine-man-top-shelf-cards-accepted"," 38.718435"," -121.290412"),
Array(" the-sanctuary-delivery-5"," 38.694406"," -121.302111"),
Array(" canna-couriers-1"," 38.68"," -121.27"));
foreach ($arr as $key => $value) {
// loop through inner array
foreach ($arr as $key2 => $value2) {
// skip if same element in array
if($key == $key2){
continue;
}
// if distance is more than 2 miles remove from array
$distance = distance($value[1],$value[2],$value2[1],$value2[2]);
// visual output
within_distance($distance, 2, $value[0], $value2[0]);
if($distance > 2){
unset($arr[$key]);
}
}
}
// show array
echo '<pre>';
print_r($arr);
echo '</pre>';
/* funtions*/
// function to calculate distance between two points
function distance($lat1, $lng1, $lat2, $lng2) {
if (($lat1 == $lat2) && ($lng1 == $lng2)) {
return 0;
}
else {
$theta = $lng1 - $lng2;
$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;
return $miles;
}
}
// function to show if distance is more than x miles
function within_distance($distance, $miles, $name, $name2) {
if ($distance > $miles) {
echo '<pre><b>'.$name .' <span style="color:green;"> is '.$distance.' from '.$name2.' </span></b></pre>';
}else{
echo '<pre><b>'.$name .' <span style="color:red;"> is '.$distance.' from '.$name2.' </span></b></pre>';
}
}
Output is
exclusive-care-2 is 1 from the-re-up-citrus-heights-orangevale
exclusive-care-2 is 3 from norcal-medicine-man-top-shelf-cards-accepted
exclusive-care-2 is 1 from the-sanctuary-delivery-5
exclusive-care-2 is 2 from canna-couriers-1
the-re-up-citrus-heights-orangevale is 2 from norcal-medicine-man-top-shelf-cards-accepted
the-re-up-citrus-heights-orangevale is 0 from the-sanctuary-delivery-5
the-re-up-citrus-heights-orangevale is 2 from canna-couriers-1
norcal-medicine-man-top-shelf-cards-accepted is 2 from the-sanctuary-delivery-5
norcal-medicine-man-top-shelf-cards-accepted is 3 from canna-couriers-1
the-sanctuary-delivery-5 is 2 from canna-couriers-1
canna-couriers-1 is 2 from the-sanctuary-delivery-5
final array:
Array
(
[3] => Array
(
[0] => the-sanctuary-delivery-5
[1] => 38.694406
[2] => -121.302111
)
[4] => Array
(
[0] => canna-couriers-1
[1] => 38.68
[2] => -121.27
)
)
The final array can't be correct because the-sanctuary-delivery-5 is under 2 miles from canna-couriers-1
I feel like as soon as the first interval ran the-re-up-citrus-heights-orangevale should have been removed from the array.