2 回答

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
我寫了一段代碼可以幫助你解決這個(gè)問題,必要時(shí)會(huì)提到注釋。希望對(duì)你有幫助。
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貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
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
}
所以根據(jù)你的問題
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
在函數(shù)內(nèi)創(chuàng)建一個(gè)常量或另一個(gè)變量,并使用該變量值循環(huán)條件。這對(duì)以后的修改很有幫助。
像這樣的東西
$loopValues = [1,2,3,4,5,6] // Or $loopValues = [['val' => 1, 'function' => functionName()]] <- I prefer this one all the time
然后像下面這樣循環(huán)
foreach ($roles as $role) {
if ($role == $loopValues[0]) {
//your logic here
}
}
- 2 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)