首页 > 解决方案 > 用于查询传递的内爆数组,只返回一个值

问题描述

我正在尝试内爆一个 ID 数组(我知道这个数组应该包含 16 个 ID),以便我可以将逗号分隔的字符串传递给查询。

我现在有这个:

foreach ($daily as &$d) {
$ids = [];

if ($d->date > $condition){
  $d->employee = 'hired';
  $ids[] = $d->empNum;
  dd($ids);
}
print_r($ids);

$empIDs = implode(" ", $ids);

endforeach

并且dd($ids)成功转储了第一个 ID,但只有那个。

我应该如何循环并正确内爆它以便我可以将它传递给查询?

标签: phparrays

解决方案


你快到了:

// Declare this outside the loop so that we do not keep overwriting it
$ids = [];

foreach($daily as &$d)
{
    if($d->date > $condition)
    {
        $d->employee = 'hired';
        $ids[] = $d->empNum;
        dd($ids);
    }
    print_r($ids);
}  // Make sure to properly close the foreach construct because endforeach should be causing errors

// Generate a comma-separated list of IDs
$empIDs = implode(",", $ids);

推荐阅读