开发者

foreach empty element in array set element in other array

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开发者_JAVA技巧 things like this etc:

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

thanks for helping out


foreach ($clean as $k => $value) {
    $errors[$k] = empty($value);
}


You can use a couple of array functions to get all keys of the items that have an error in a new array:

$data = array(
    'pers_anrede' => 'Frau',
    'pers_titel' => null,
    'pers_vorname' => 'BLa',
);

$filtered = array_filter($data);
$error = array_combine(array_keys(array_diff_key($data, $filtered)),
                       array(true));

And then:

if(isset($error['pers_titel'])) {
    // blah
}

See it in action.


That should be easy with one more foreach loop.

Assuming you are storing the array in $array.

foreach($array as $key => $value) {
 if($value == NULL){
  $array[$key] = true;
 }else{
  $array[$key] = false;
 }
}


In order to do your validation, you will still need to foreach on $error array.

So it is simpler to do your validation like this:

foreach ($clean as $key=>$value) {
    if (empty($value)) {
        $error_message .= "$key field is required<br/>";
    }
}


simply use

foreach ($clean as $k => $value) {
    $validate[$k] = ($value == "")?true:false;
}

empty or default array_filter functions will return false for zero values which you may not want.

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)


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 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜