我在一个绑定中,多次在不同的表单项的页面上,我只在表单项中插入一个css div类,如果error_array键存在,这是我如何突出显示其中有错误的表单项。如何检查数组键是否存在如果数组不存在PHP
工程,很好,如果我有一个错误,因为那么我的错误数组已设置,问题是在设置错误之前,它会尝试查找不存在的数组键。我想使用php的isset函数首先是票,但我想你不能将isset与另一个函数结合起来?
<?php
//this works
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)){
echo "good";
}
// this does not work, will give write errors
$search_array = array('first' => 1, 'second' => 4);
if (isset(array_key_exists('first', $search_array))){
echo "good";
}
// Here is 1 example how I need to make the end result work
$country_class = (array_key_exists('country', $signup_errors)) ? ' signup_error' : ' signup_good';
echo '<select name="country" id="country" class="textarealong ' .$country_class. '"/>';
?>
在其他地方我像这样使用
<?PHP echo(array_key_exists('password', $signup_errors)) ? ' signup_error' : ' signup_good';?>
,我需要有它是可能的话
另外关于您的评论,没有这个初始化这个变量? – JasonDavis 2009-09-03 19:57:51
不,它不会,因为即使'array_key_exists'自动生成$ signup_errors(并且我非常肯定它没有),那么'isset()'的失败将导致它不会运行。 'isset()'显然不允许对它测试的变量进行变形。 – Guss 2009-09-04 00:17:45