X
X
XenK2017-08-10 12:46:08
Yii
XenK, 2017-08-10 12:46:08

Correct foreach?

There is such an array:

Array
(
    [6] => Array
        (
            [sma] => Array
                (
                    [0] => Sma must be a number.
                )

            [bet] => Array
                (
                    [0] => bet should contain at most 3 characters.
                )

        )

    [10] => Array
        (
            [dest] => Array
                (
                    [0] => dest must be a number.
                )

            [bet] => Array
                (
                    [0] => bet should contain at most 3 characters.
                )

        )

    [11] => Array
        (
            [bet] => Array
                (
                    [0] => bet should contain at most 3 characters.
                )

        )

)

It is necessary to send flash messages, which should contain keys (6, 10, 11) and the error itself (for example: Sma must be a number.", I use:
Yii::$app->getSession()->addFlash(...);
Tell me, how to write a loop for such a send?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2017-08-10
@XenK

foreach ($errors as $key=>$list) {
    foreach ($list as $error) {
        Yii::$app->getSession()->addFlash($key, $error);
    }
}

S
Stalker_RED, 2017-08-10
@Stalker_RED

$result = json_encode($my_array_with_errors, JSON_FORCE_OBJECT);

php.net/manual/ru/function.json-encode.php
And foreach is not needed in this case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question