简体   繁体   中英

php in_array best way to check an array?

I have the following array:

if ( empty($a)||empty($b)||empty($c)){
    if(empty($a)){
        $errors[]="a is empty";
    }
    if(empty($b)){
        $errors[]="b is empty";
    }
    if(empty($c)){
        $errors[]="c is empty";
            }
 }...

How can I check with if (in_array('??'; $errors)) if the array is filled with the $a , $b or $c error message?

I know about this way:

$errors = array(
    'a' => 'Please enter a.',
    'b' => 'Please enter b.',
    'c' => 'Please enter c.'
);

Here I can simply check with if (in_array('a'; $errors)) if there is some error message for a or not. The problem I have is, that I not only have one error-message for a,b or c. so that I look for a way like this that combines both methods:

$errors = array(
        'a' => if ( empty ($a) || $specific_error1_for_a  || $specific_error2_for_a ),
        'b' => if ( empty ($b) || $specific_error1_for_b  || $specific_error2_for_b ),
        'c' => if ( empty ($c) || $specific_error1_for_c  || $specific_error2_for_c ),
    );

I'm looking for a way to search the array errors[] for instances of failure messages for each of these elements a,b or c .

The main problem is that I would like to have one variable or something else, which I can search for when using in_array. To get more specific:

I have a errorlayer for each of my input fields. Therefore I need to search the whole array errors[] if there is a specific error-message for the specific input field:

<input type="text" id="a" name="a" value="<?php echo isset ($_POST['a'])? $_POST['a'] : ''; ?>" tabindex="10" autocomplete="off"/><?php if (**in_array(...., $errors)**):?><span class="error"><?php echo $errors['a'];?></span><?php endif;?>

The problem is, like I already said, I have more than only one instance of error-message for each input field so that I would have something like this:

(**in_array('a is empty' || 'a is too short' || 'a is too long' ..., $errors)**)

That's why I thought it would be better to search for just one variable like that:

(**in_array($a, $errors)**)

I would really appreciate it if there is someone who could give me advise on this. Thanks a lot.

array_intersect can be used much like an in_array for multiple values:

if(array_intersect($errors, array(
    'a is empty',
    'specific_error1_for_a',
    'specific_error2_for_a',
))) {
    // There is an error for a
}

However, I would recommend designing your program differently. If you store the errors in an associative array to begin with, then checking if there are any errors for a given variable becomes much more efficient:

if(empty($a)){
    $errors['a'][]="a is empty";
}
if(empty($b)){
    $errors['b'][]="b is empty";
}
if(empty($c)){
    $errors['c'][]="c is empty";
}

...

if(isset($errors['a'])) {
    // There is an error for a
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM