注意:使用 Foreach 时数组到字符串的转换


Notice: Array to string conversion when using Foreach

所以我正忙于在这个系统上更改您的密码,当密码更改时,会出现一个绿色框,其中包含消息"您的密码已更改"。

出现错误时,将显示一个包含错误的红色框,例如:"您的当前密码不正确"或"您的新密码和验证密码不同"。

所以当有一个 post 请求时,我count错误,如果超过 0,则返回如下错误:

public function nieuwpw($username, $currentpass, $newpass, $newpassconf)
{
    $this->errors[] = array();
    $this->succes[] = array();
    $query = $this->db->conn->prepare('SELECT pass FROM ht_users WHERE naam = ?');
    $query->bind_param('s', $username);
    $query->execute();
    $query->bind_result($dbpass);
    $query->store_result();
    while ($query->fetch()) {
        if (password_verify($currentpass, $dbpass))
        {
            if ($newpass === $newpassconf)
            {
                $newpasshash = password_hash($newpass, PASSWORD_DEFAULT);
                $stmt = $this->db->conn->prepare('UPDATE ht_users SET pass = ? WHERE naam = ?');
                $stmt->bind_param('ss', $newpasshash, $username);
                $stmt->execute();
                $stmt->close();
            }
                else
            {
                $this->errors[] = 'Whoops, je 2 nieuwe wachtwoorden zijn niet gelijk.';
            }
        }
            else
        {
            $this->errors[] = 'Whoops, je huidige wachtwoord klopt niet!';
        }
    }
    if (count($this->errors) > 0)
    {
        return $this->errors;
    }
    $this->succes[] = 'Je wachtwoord is met success gewijzigd!';
    return $this->succes;
    $query->close();
}

}

这是我用来更改密码的功能。以下是我如何"回显"错误:

        <?php //24
//25
if(isset($users->succes))//26
{//27
    if (count($users->succes) > 0) //28
    {//29
        foreach ($users->succes as $succes) {//30
                    $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $succes. '</p></div>';//31
    echo $content;//32
//33
            }   //34
    }//35
}//36
if(isset($users->errors))//37
{//38
    if (count($users->errors) > 0) //39
    {//40
        foreach ($users->errors as $errors) {//41
                    $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $errors. '</p></div>';//42
    echo $content;//43
//44
            }   //45
    }//46
}//47
//48
?> //49

尽管如此,即使我只回显错误或成功消息,如果超过 1,我也会收到这些错误:

Notice: Array to string conversion in C:'xampp'htdocs'pages'page16.php on line 31

Notice: Array to string conversion in C:'xampp'htdocs'pages'page16.php on line 42

在第二个代码块中,我对行进行了编号,以便您可以看到哪些行抛出错误。

var_dump时,我得到以下输出:

array(2) { [0]=> array(0) { } [1]=> string(41) "Whoops, je huidige wachtwoord klopt niet!" }

另外,我觉得非常奇怪的是,它两次显示红色错误框,即使没有任何成功消息,它也显示了绿色框......

我希望这将被视为一个完整的问题,有人可以帮助我解决这个问题。谢谢。

我认为错误的原因是您发布的第一个代码块的第 3 行和第 4 行:

$this->errors[] = array();
$this->succes[] = array();

尝试将这些行更改为:

$this->errors = array();
$this->succes = array();

在代码中,创建一个数组,并立即将数组的第一个元素设置为数组。您可以在您发布var_dump中看到这一点:

array(2) { [0]=>

array(0) { } [1]=> string(41) "Whoops, je huidige wachtwoord klopt niet!" }

稍后,当您遍历 $errors 或 $succes 数组时,循环的第一次迭代会返回您设置为$errors或$succes数组的第一个元素的数组。然后你尝试回显该数组,php 会通知你正在回显一个数组。

我建议的修复程序是否有效,您是否明白为什么您的代码会损坏?

附注:

  1. 尽量将所有代码保留为英语,以便非荷兰语人士更容易理解您的代码。
  2. 以下字符串用荷兰语书写:"Je wachtwoord is met success gewijzigd!"。在荷兰语中,"succes"用1个"s"写成,所以它应该是"Je wachtwoord is met succes gewijzigd!"。

祝你好运!

你试过这样使用它吗?

$this->errors = array();
$this->succes = array();

    foreach ($users as $user) {
        if(isset($user->succes))
        {
            $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' .$user->succes. '</p></div>';
            echo $content;
        }  
        if(isset($user->errors))
        {
            $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $user->errors. '</p></div>';
            echo $content;
        }   
    }

评论,如果有任何错误...