What is the best way to iterate through and display only the error messages in the nested array using a foreach loop in PHP?

When iterating through a nested array in PHP using a foreach loop, you can access the inner arrays by nesting additional foreach loops. To display only the error messages in the nested array, you can check if the current value is an array and then iterate through it to find the error messages.

$errors = [
    "error1",
    "error2",
    ["error3", "error4"],
    ["error5", "error6"]
];

foreach ($errors as $error) {
    if (is_array($error)) {
        foreach ($error as $innerError) {
            echo $innerError . "<br>";
        }
    } else {
        echo $error . "<br>";
    }
}