从php中的复杂数组中获取元素名称

从php中的复杂数组中获取元素名称

问题描述:

My php knowledge is not enough to deal with such a complex array. I am geting it in my Zend Framework 2.3 app while form validation, and want to list all errors, not show each in below input field.

This is an array that I got

Array ( 
       [email] => Array ( 
                         [isEmpty] => Value is required and can't be empty 
                         [someElse] => Some other message
                         ) 
       [subject] => Array ( 
                           [stringLengthTooLong] => The input is more than 128 characters long 
                           ) 
       [content] => Array ( 
                           [isEmpty] => Value is required and can't be empty 
                           ) 
       )

and this is what i have tried so far

i know there is something like Key() but is this what i should be using?

I want to get an output

email - Value is required and...
email - Some other message
Subject - THe input is more than...
content - Value is ..

So far i have tried

if(isset($errorsMessages)) {
    while (current($errorsMessages)) {
        echo key($errorsMessages);
        next($errorsMessages);  
    }
}

it works i am getting list email, subject, content. But then i want to read certain sub-arrays. I have tryied

if(isset($errorsMessages)) {
    while ($fruit_name = current($errorsMessages)) {
        echo key($errorsMessages);
            foreach ($errorsMessages as $i) {
                foreach($i as $j) {
                    echo '<br ./>' . ' - ' . $j;
                }
            }
        next($errorsMessages);  
    }
}

but it is a totall mess, i am getting output

email
- Value is required and can't be empty
- The input is more than 128 characters long
- Value is required and can't be empty

I'm using zf2 validation too.

If you are trying to accomplish a direct way to check the array content, you should try it on hand by yourself.

A way to easy is to check:

$validatorMessages //suppost the messages are there.

$messageInline = ''; //suppost to be the message without array

//add validation by yourself to check if validatorMessage was not a empty array, becouse it throw a exception if has 0 itens.

foreach($validatorMessage as $input => $messages)
{
    if(count($messages)>0)) { //check if really was a first message

        foreach($messages as $type => $message) { //Here is the magic!

            $messageInline .= sprintf('%s - %s',$input,$message).'<br>'; //setting message and newline.
        }
//please note that the first param pass to sprintf was Input, from the master array indice, and the message was a subsequent item inside the same input value.
    }
 }

if(strlen($messageInline)>0) {
     echo 'Errors Founds: ' . $messageInline;
     return false;
} else {
    return true;
}

Assuming that the data is only ever two layers deep you could nest 2 foreach loops like this foreach ($errors => $key1 as $value1) { foreach ($errors => $key2 as $value2) { echo $key1 . " - " . $value2; } }