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
实际上斜面明白你在做什么,即使 –