所以我从数据库中获取对象,并且我想将id和点放在数组中。 每次getIscorrect等于true时,id的值必须增加1。
$answers = array();
$return = $this->_quizMapper->getAll();
foreach($return as $answer){
if($answer->getIscorrect() == true){
if(!($answers[$answer->getID()])){
$answers[$answer->getID()] = 1;
}
else{
$newPoints = $answers[$answer->getID()] + 1;
$answers[$answer->getID()] = $newPoints;
}
}
}
但我一直在收到错误:PHP注意:未定义的索引:在这一行if(!($answers[$answer->getID()])){
答案 0 :(得分:0)
将其更改为if (!isset($answers[$answer->getID()]))
这将取消通知。
答案 1 :(得分:0)
这是因为没有定义密钥。
你应该使用的是:
if(!isset($answers[$answer->getID()]) || !($answers[$answer->getID()])){
答案 2 :(得分:0)
将您的代码更改为:
$answers = array();
$return = $this->_quizMapper->getAll();
foreach($return as $answer)
{
if($answer->getIscorrect() == true)
{
if(!isset($answers[$answer->getID()]))
{
$answers[$answer->getID()] = 1;
}
else
{
$newPoints = $answers[$answer->getID()] + 1;
$answers[$answer->getID()] = $newPoints;
}
}
}
您必须使用方法isset()
来检查数组中是否存在当前密钥。