tags:

views:

47

answers:

3
<?PHP
$signup_errors = array();
$signup_errors['captcha'] = 'test 1';
$signup_errors['something'] = 'test 2';
$signup_errors['another'] = 'test 3';
$signup_errors['getthepoint'] = 'test 4';

//this would work
if (isset($signup_errors) && in_array('test 4', $signup_errors)){
    echo 'it works';
} 

//However I need something like this to work
if (isset($signup_errors) && in_array('captcha', $signup_errors)){
    echo 'it works';
} 
?>

the ultimate goal here is to have a html form where I can change the css div class name is there is an error array item that exist, so if it comes back with this

$signup_errors['captcha'] = 'Please enter the correct security code';

Then on a form filed I would have something like this

<input type="text" class="textarealong <?PHP if (isset($signup_errors) && in_array('captcha', $signup_errors)){echo 'error-class';}?> " value=''>
+1  A: 

Ok I think I got it, php key exist seems to do the trick

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>
jasondavis
+2  A: 

Instead of in_array, use array_key_exists

<input type="text" class="textarealong <?PHP if (isset($signup_errors) && array_key_exists('captcha', $signup_errors)){echo 'error-class';}?> " value=''>
Doug Hays
+1  A: 

In your case I'd use

isset($search_array['first']);

instead of

array_key_exists('first', $search_array);

It's probably faster and more readable for me.

Kamil Szot