我想获取两列的值并同时循环其中两列并使用它来计算纬度和经度的距离,但问题是它为所有纬度进行循环以获得力学和循环所有力学的经度,但两者应合二为一。
$mechanicsLatitude = DB::table("tranxav_mechanics")->get(['latitude','email']);
$mechanicsLongitude = DB::table("tranxav_mechanics")->get(['longitude', 'email']);
foreach ($mechanicsLatitude as $latitudeData){
foreach ($mechanicsLongitude as $longitudeData){
$theta = $driverLongitude - $longitudeData->longitude;
$dist = sin(deg2rad($driverLatitude)) *
sin(deg2rad($latitudeData->latitude))
+ cos(deg2rad($driverLatitude))
* cos(deg2rad($latitudeData->latitude))
* cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper("K");
if ($unit == "K") {
return $miles = $miles * 1.609344;
} elseif ($unit == "N") {
$miles = $miles * 0.8684;
} else {
$miles;
}
TranxavTransaction::create([
'dEmail' => $request->get("email"),
'mEmail' => $longitudeData->email,
'car_model' => $model,
'how_it_happened' => $request->get("likelyProblem"),
'latitude' => $request->get("latitude"),
'longitude' => $request->get("longitude"),
'problem' => input::get('likelyProblem')
]);
}
}
答案 0 :(得分:0)
您可以使用索引进行简单的for循环,而不是使用foreach:
for ($i = 0; $i < $mechanicsLatitude->count(); $i++)
$theta = $driverLongitude - $mechanicsLongitude[$i]->longitude;
.
.
.
确保 $ mechanicsLatitude 和 $ mechanicsLongitude 具有相同的长度。