爲什麼我會在$errors[$field] .= '<div class="error"><p>' . $error . '</p></div>';
這行上的代碼中得到'Illegal offset type'錯誤?爲什麼在此代碼中出現錯誤'Illegal offset type'?
代碼:
$errors = [];
foreach ($validation->errors() as $field)
{
$errors[$field] = '';
foreach ($field as $error) {
$errors[$field] .= '<div class="error"><p>' . $error . '</p></div>';
}
}
驗證:
private function addError($field, $error)
{
$this->errors[$field][] = $error;
}
public function errors()
{
return $this->errors;
}
解決方案:
foreach ($validation->errors() as $key => $fieldErrors)
{
$errors[$key] = '';
foreach ($fieldErrors as $error) {
$errors[$key] .= '<div class="error"><p>' . $error . '</p></div>';
}
}
因爲數組不能成爲陣列的密鑰。 –