2 回答
TA贡献2065条经验 获得超14个赞
我写了一段代码可以帮助你解决这个问题,必要时会提到注释。希望对你有帮助。
foreach ($roles as $role) {
if ($role == 2) {
$data['need_app'][] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id); // you need to make an array here otherwise the current value will over-write the previous one
var_dump(count($data['need_app']));
}else if ($role == 3) {
$data['need_app'][] = $this->Hire_model->need_approval_recruiter(); // you need to make an array here otherwise the current value will over-write the previous one
}else if ($role == 4) {
$data['need_app'][] =$this->Hire_model->need_approval_hr(); // ...
}else if ($role == 5) {
$data['need_app'][] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id); // ...
}
// var_dump($data['need_app']);
}
TA贡献1798条经验 获得超7个赞
foreach ($roles as $role) {
if ($role == 2) {
//your logic here
} else if ($role == 4) {
//your logic here
} else if ($role == WHAT_EVER_YOU_WANTS) {
//Logic
}
//your logic here
}
所以根据你的问题
foreach ($roles as $role) {
if ($role == 2) {
$data['need_app'] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id);
} else if ($role == 3) {
$data['need_app'] = $this->Hire_model->need_approval_recruiter();
} else if ($role == 4) {
$data['need_app'] = $this->Hire_model->need_approval_hr();
} else if ($role == 5) {
$data['need_app'] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id);
}
}
而不是使用类似$role == 4
在函数内创建一个常量或另一个变量,并使用该变量值循环条件。这对以后的修改很有帮助。
像这样的东西
$loopValues = [1,2,3,4,5,6] // Or $loopValues = [['val' => 1, 'function' => functionName()]] <- I prefer this one all the time
然后像下面这样循环
foreach ($roles as $role) {
if ($role == $loopValues[0]) {
//your logic here
}
}
- 2 回答
- 0 关注
- 139 浏览
添加回答
举报