我有一个$ mentor_fails数组,该数组跟踪未能插入数据库表的指导者。虽然,我遇到了插入失败的异常,但是我的$ mentor_fails似乎没有将包含失败的指导者信息的对象保存到$ mentor_fails中。请让我知道我做错了。
function assign_mentors_and_projects($mentors,$projects){
global $mydb;
$mentor_fails = array();
foreach($mentors as $mentor){
foreach($projects as $project){
try {
$update_mentors_and_projects_rlshp = $mydb->insert(
'abcdefg',
array('project_id'=>$project, 'mentor_id'=>$mentor),
array('%s','%s'));
if($update_mentors_and_projects_rlshp == false) {
array_merge($mentor_fails, array('mentor_email'=>$mentor, 'project_id'=>$project));
throw new Exception("exist already:(mentor-".$mentor." project_id-".$project.")\r\n");
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
return $mentor_fails;
}`
答案 0 :(得分:1)
我认为问题在于array_merge不能通过引用合并。
来自PHP doc:
array_merge(array $ array1 [,array $ ...]):array
此代码段可以解决您的问题。
$mentor_fails= array_merge($mentor_fails, array('mentor_email'=>$mentor, 'project_id'=>$project_id));
我看到您可以添加到$ mentor_fails中,所以您可以像这样:
$mentor_fails[]= array('mentor_email'=>$mentor,'project_id'=>$project_id);
答案 1 :(得分:-2)
收件人:
array_merge($mentor_fails, array('mentor_email'=>$mentor, 'project_id'=>$project));
throw new Exception("exist already:(mentor-".$mentor." project_id-".$project.")\r\n");
来自:
throw new Exception("exist already:(mentor-".$mentor." project_id-".$project.")\r\n");
array_merge($mentor_fails, array('mentor_email'=>$mentor, 'project_id'=>$project));