2009-09-03 7 views
0
<?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'; 
} 
?> 

le but ultime est d'ici un formulaire HTML où je peux changer le nom de la classe css div est il y a un élément de tableau d'erreur qui existe, si elle revient avec cetteComment puis-je faire cela avec des tableaux PHP?

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

Puis une forme déposée j'aurais quelque chose comme ça

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

Répondre

2

Au lieu de in_array, utilisez array_key_exists

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

Ok je pense que je l'ai eu, php existe clé semble faire l'affaire

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

Dans votre cas j'utiliser

isset($search_array['first']); 

au lieu de

array_key_exists('first', $search_array); 

Il est probablement plus rapide et plus facile à lire pour moi.

Questions connexes