php foreach bug。任何想法为什么它不能正常工作?

时间:2016-03-21 15:51:25

标签: php foreach

为什么会继续?


    foreach($questionsArr as $element){
        foreach($answersArr as $key => &$answer){
            if($key == $element["id"]){
                $answer["question"] = $element["question_text"];
            }
        }
    }
    $loggedInUser = user::getLoggedInUserObject();
    $userEmail = $loggedInUser->getEmail();
    $emailBody = '';
    $emailBody .= "".$tUser->getFname()." ".$tUser->getLName()." answers:

"; dump($answersArr); foreach($answersArr as $key => $answer){ $emailBody .= "

".$answer["question"]."

".$answer["answer"]."

"; } $emailBody .= ''; dump($emailBody); die();



<pre style='margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>$ => <span style='color:#a2a2a2'>Array (4)</span><br>(<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['1'] => <span style='color:#a2a2a2'>Array (2)</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; (<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['answer'] = <span style='color:#a2a2a2'>String(6)</span> <span style='color:green'>"asdasd"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['question'] = <span style='color:#a2a2a2'>String(31)</span> <span style='color:green'>"WHAT WENT WRONG WITH THIS CALL?"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; )<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['2'] => <span style='color:#a2a2a2'>Array (2)</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; (<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['answer'] = <span style='color:#a2a2a2'>String(7)</span> <span style='color:green'>"asdasda"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['question'] = <span style='color:#a2a2a2'>String(58)</span> <span style='color:green'>"WHAT COULD YOU HAVE DONE DIFFERENTLY TO IMPROVE THIS CALL?"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; )<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['3'] => <span style='color:#a2a2a2'>Array (2)</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; (<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['answer'] = <span style='color:#a2a2a2'>String(9)</span> <span style='color:green'>"sdasdasda"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['question'] = <span style='color:#a2a2a2'>String(34)</span> <span style='color:green'>"WHAT DID YOU LEARN FROM THIS CALL?"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; )<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['4'] => <span style='color:#a2a2a2'>Array (2)</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; (<br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['answer'] = <span style='color:#a2a2a2'>String(6)</span> <span style='color:green'>"qweqwe"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; <span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ['question'] = <span style='color:#a2a2a2'>String(68)</span> <span style='color:green'>"WHAT ARE YOU GOING TO DO TO CORRECT THE AREAS OF FOCUS ON THIS CALL?"</span><br><span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; )<br>)<br></pre><pre style='margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>$ = <span style='color:#a2a2a2'>String(325)</span> <span style='color:green'>"<html><body><p style="font-size:13px">ADRIANA CARDENAS answers:</p><p>WHAT WENT WRONG WITH THIS CALL?</p><p>asdasd</p><p>WHAT COULD YOU HAVE DONE DIFFERENTLY TO IMPROVE THIS CALL?</p><p>asdasda</p><p>WHAT DID YOU LEARN FROM THIS CALL?</p><p>sdasdasda</p><p>WHAT DID YOU LEARN FROM THIS CALL?</p><p>sdasdasda</p></body></html>"</span><br></pre>
&#13;
&#13;
&#13;

var_dump for the code

任何想法?

1 个答案:

答案 0 :(得分:1)

因为您在没有阅读relevant PHP docs的情况下通过引用而使用了可能产生的副作用.....这不是一个错误,它是记录在案的行为

请参阅警告说明

  

警告   即使在foreach循环之后,$ value和最后一个数组元素的引用仍然存在。建议通过unset()销毁它。

注意它所说的内容,并取消设置

foreach($questionsArr as $element){
    foreach($answersArr as $key => &$answer){
        if($key == $element["id"]){
            $answer["question"] = $element["question_text"];
        }
    }
    unset($answer);
}

或者首先不要通过引用使用

foreach($questionsArr as $element){
    foreach($answersArr as $key => $answer){
        if($key == $element["id"]){
            $answersArr['key']["question"] = $element["question_text"];
        }
    }
}