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

Addaing a value to an existing key in associative array

所以我正在尝试向关联数组中的现有键添加一个值。键 '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',
        ]
    ];