传递查询的放大数组,仅返回一个值

时间:2018-08-06 13:39:30

标签: php arrays

我正在尝试使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,但只有一个。

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

1 个答案:

答案 0 :(得分:1)

您快到了:

// 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);