$row["distance"] = distanceBetween($row["location_y"], $row["location_x"], $lng, $lat); $row["distance"] = round($row["distance"] / 1000, 2); function distanceBetween($longitude1, $latitude1, $longitude2, $latitude2) { $radLat1 = radian($latitude1); $radLat2 = radian($latitude2); $a = radian($latitude1) - radian($latitude2); $b = radian($longitude1) - radian($longitude2); $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); $s = $s * 6378.137; $s = round($s * 10000) / 10000; return $s * 1000; } function radian($d) { return ($d * 3.1415926535898) / 180; }