dongnuo4594 2014-06-24 17:02
浏览 46
已采纳

从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
  • 写回答

2条回答 默认 最新

  • dongqi9125 2014-06-24 17:09
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)