0
爲什麼$ poll_id在array_map的回調函數中param爲NULL(undefined)?下面的代碼工作,但我不得不使用專門的私人的$ id類成員克服它...將參數傳遞給內函數
class Polls_model extends CI_Model
{
private $id;
// ...
public function add_poll_answers($poll_id, $answers)
{
$this->id = $poll_id;
if (count($answers) > 0)
{
$this->db->insert_batch('poll_answers',
array_map(
function ($a)
{
log($poll_id); // NULL, why?
log($this->id); // correct value
return ['name' => $a,'poll_id' => $this->id];
}, $answers));
}
}
}
因爲你指定'$ poll_id'到'$這個 - > id'代碼中的'$這個 - > ID = $ poll_id;' – Saty
實際上斜面明白你在做什麼,即使 –