Bu PHP dizileri ile nasıl yapabilirim?

3 Cevap php
<?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';
} 
?>

Burada nihai hedefi ben css div class adını değiştirebilirsiniz bir html formu var olan mevcut bir hata dizi öğe olduğu, bu yüzden bu geri gelirse

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

Ardından açılan bir form üzerinde böyle bir şey olurdu

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

3 Cevap

Bunun yerine İn_Array kullanın array_key_exists

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

Tamam ben hallederim düşünüyorum, php anahtar mevcut hile yapmak gibi görünüyor

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

Senin durumunda ben kullanmak istiyorum

isset($search_array['first']);

yerine

array_key_exists('first', $search_array);

Muhtemelen daha hızlı ve daha kolay okunabilir benim için.