我有一个数组$rest_id
有3个ID,但是当我做foreach并把它放在我的sql查询中时,我调试它时只出现一个值。这是代码。
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
}
答案 0 :(得分:0)
我认为你需要这样的东西,如果你想从3 ids中找到最近的餐馆:
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
}
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
答案 1 :(得分:0)
您需要使用$ids = implode(',',$ID)
函数将数组转换为字符串。
然后,您可以在查询中使用where id in ($ids)