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
may be useful (since it's similar to in_array with multiple values), but I'm not entirely clear on what you're trying to do here. – Brilliand Jul 20 '12 at 10:00