将值添加到关联数组中的现有键



因此,我试图向关联数组中的现有键添加一个值。密钥"message"用于根据验证显示不同的错误。

$formStatus = [
'success' => false, 
'message' => "Phone number validation failed"
];

如果假设电子邮件验证失败,我想稍后在同一密钥上添加另一条消息。这可能吗?

you can also use **array_replace()** function
$formStatus = [
'success' => false, 
'message' => "Phone number validation failed"
];
print_r($formStatus); //printing array for checking
$formStatus = array_replace($formStatus, array("message" => "Email validation failed"));
print_r($formStatus); //printing array for checking

您可以这样更改$formStatus:

$formStatus = [
'success' => false,
'validation' => [
'phone_message' => 'Phone number validation failed',
'email_message' => 'Email validation failed',
]
];

相关内容

  • 没有找到相关文章

最新更新