我正在嘗試使用回圈來獲取 Laravel 中多個地址的距離和時間
foreach ($driver as $product) {
$googlePickupDistanceMatrix = new GoogleDistance(
$product->current_latitude,
$product->current_longitude,
$sendLAT,
$sendLNG,
);
$pickupDistanceAndTime2 = $googlePickupDistanceMatrix->distanceAndTime();
}
$driver 變數包含這個
0 => App\Driver {#317 ?}
1 => App\Driver {#300 ?}
2 => App\Driver {#281 ?}
上面的代碼回傳一個地址的單個緯度和經度,我找不到其他地址,請幫助。
uj5u.com熱心網友回復:
您將距離請求的結果存盤在標量變數中,因此會覆寫所有結果,直到您只能看到最后一個結果。
foreach ($driver as $product) {
$googlePickupDistanceMatrix = new GoogleDistance(
$product->current_latitude,
$product->current_longitude,
$sendLAT,
$sendLNG,
);
$pickupDistanceAndTime2[] = $googlePickupDistanceMatrix->distanceAndTime();
// change ^^
}
如果你得到
Array ( [0] => Array ( [0] => 4 [1] => 15 )
[1] => Array ( [0] => 4 [1] => 15 )
[2] => Array ( [0] => 18 [1] => 29 )
)
它們是包含 3 個結果的 3 個陣列出現,所以另一個 foreach
foreach ( $pickupDistanceAndTime2 as $dt ){
echo 'Distance ' . $dt[0] . 'and Time ' . $dt[1] . '<br>';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/512461.html
標籤:php拉拉维尔
下一篇:頁面重繪時插入SQLite
