php - 用于查询传递的内爆数组,只返回一个值
问题描述
我正在尝试内爆一个 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,但只有那个。
我应该如何循环并正确内爆它以便我可以将它传递给查询?
解决方案
你快到了:
// 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);
推荐阅读
- apache-kafka - 如何将 Kafka 连接到 Thingsboard 平台
- xpath - 使用 ImportXML 将来自 goodreads.com 的书名导入 Google 表格有时会出现“导入内部错误”
- database - 在 char(2) 列上使用 trim 并且数据库字段只有空格时,Oracle Select 查询不返回任何结果
- c - 试图删除 malloc 的使用 - 这里有替代方案吗?
- kotlin - 线程何时会挂起挂起功能?
- javascript - 如何将两张图片合成一张图片并在 Javascript 中更改颜色?
- spring-boot - 在 Spring Boot 中根据登录用户更改应用程序的 URL
- swift - Swift 可重用模型类/结构
- php - 当我已经在我的 Web.php 中定义了资源控制器时,没有定义路由 [controller.create]
- python - S3 Kedro 数据集是线程安全的吗?