How can you dynamically add new error messages to the nested array in PHP?

To dynamically add new error messages to a nested array in PHP, you can use the array_push() function to add new elements to the nested array. You can specify the key of the nested array where you want to add the new error message and then push the message onto that array.

$errors = [
    "validation_errors" => [
        "email" => ["Email is required"],
        "password" => ["Password is required"]
    ]
];

$newErrorMessage = "Username is required";

array_push($errors["validation_errors"], ["username" => [$newErrorMessage]]);

print_r($errors);