<?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';
}
?>
这里的最终目标是有一个html表单,我可以在其中更改css div类名称是否存在一个错误数组项,所以如果它带回来
$signup_errors['captcha'] = 'Please enter the correct security code';
然后在表格上我会有类似的东西
<input type="text" class="textarealong <?PHP if (isset($signup_errors) && in_array('captcha', $signup_errors)){echo 'error-class';}?> " value=''>
答案 0 :(得分:2)
<input type="text" class="textarealong <?PHP if (isset($signup_errors) && array_key_exists('captcha', $signup_errors)){echo 'error-class';}?> " value=''>
答案 1 :(得分:1)
好吧我觉得我明白了,php键存在似乎可以做到这一点
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
答案 2 :(得分:1)
在你的情况下,我会使用
isset($search_array['first']);
而不是
array_key_exists('first', $search_array);
对我来说,它可能更快,更具可读性。