donglang1976 2011-09-09 11:57
浏览 8
已采纳

foreach其他数组中的数组集元素中的空元素

i am fetching some $_POST vars and put them in an array likes this:

foreach (array_keys($_POST) as $key) {
    $clean[$key] = mysql_real_escape_string($_POST[$key]);
}

the result is an array like this:

Array
(
    [pers_anrede] => Frau
    [pers_titel] => Dr.
    [pers_vorname] => BLa
    [pers_nachname] => blablabla
    [pers_vorwahl] => 0123
    [pers_telefon] => 3456789
    [job_bundesland] => Berlin
    [job_plz] => 
    [job_ort] => 
    [job_str] => 
)

now, what i want to do is some form of simple validation. if an element of the array above has any value, its error is set to false. if a value for the element is missing, its error is set to true. i would like to get an array like this (keeping the example data from above, only the last three elements would produce an error:

Array
(
    [pers_anrede] => false
    [pers_titel] => false
    [pers_vorname] => false
    [pers_nachname] => false
    [pers_vorwahl] => false
    [pers_telefon] => false
    [job_bundesland] => false
    [job_plz] => true
    [job_ort] => true
    [job_str] => true
)

with this array i want to be able to do things like this etc:

if $error['pers_anrede'] == true {
      $error_message = 'please correct blabla';
}

thanks for helping out

  • 写回答

6条回答 默认 最新

  • dqz7636 2011-09-09 12:01
    关注

    This will loop through your clean array's keys and load an error array that stores true if they are empty:

    foreach ( array_keys( $clean ) as $key )
        $error[$key] = empty( $clean[$key] );
    

    or if you don't want to use array keys:

    foreach ( $clean as $key => $value )
        $error[$key] = empty( $value );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应