[英]isset() or array_key_exists() doesnt work with $_POST
I have a VERY simple code, I dont know what I am doing wrong. 我有一个非常简单的代码,我不知道我做错了什么。 I am sending post variables with Ajax, there are 3 checkboxes (an array) name="options[]".. I am receiving the right array, if I check Option1, Option2 or Option3, I get the correct Array.. but when I try to check and confirm them with isset() or with array_key_exist() it does not give me the right answers.. For example, if I only check Option 1 and 3, I get the following in the POST 我用Ajax发送post变量,有3个复选框(数组)name =“options []”..我收到正确的数组,如果我检查Option1,Option2或Option3,我得到正确的数组..但是当我尝试使用isset()或使用array_key_exist()检查并确认它们,但它没有给我正确的答案。例如,如果我只检查选项1和3,我在POST中得到以下内容
[options] => Array
(
[0] => option_one
[1] => option_two
)
I need to do some action only IF the certain key does NOT exist in the array.. I tried something like below.. 我只需要在数组中不存在某个键时才需要执行某些操作..我尝试过类似下面的内容。
if (array_key_exists("option_one", $_POST['options'])) {
echo "Option one exists";
} else {
echo "option one does not exist";
}
it returns flase results, then I tried using ! 它返回flase结果,然后我尝试使用! (not) before it, that returns the same result. (之前),返回相同的结果。 Then I tried with isset($_POST['options']['option_one']) without any luck.. then I tried the following function to check again within the array.. 然后我尝试使用isset($ _ POST ['options'] ['option_one'])没有任何运气..然后我尝试了以下函数在数组中再次检查..
function is_set( $varname, $parent=null ) {
if ( !is_array( $parent ) && !is_object($parent) ) {
$parent = $GLOBALS;
}
return array_key_exists( $varname, $parent );
}
and used it like this 并像这样使用它
if (is_set("option_one", $_POST['options'])) {
echo "Option one exists";
} else {
echo "option one does not exist";
}
nothing works, it simply returns false value. 什么都行不通,它只是返回假值。 I tried with $_REQUEST instead if $_POST but no luck, I have read many threads that isset() or array_key_exists() returns false values.. What is the solution for this ? 我尝试使用$ _REQUEST而不是$ _POST但没有运气,我已经阅读了许多线程,isset()或array_key_exists()返回错误的值。这是什么解决方案? any help would be highly appreciated.. I am tired of it now.. 任何帮助都将受到高度赞赏..我现在已经厌倦了..
regards 问候
option_one
is not a key in the array, it's a value . option_one
不是数组中的键 ,它是一个值 。 The key is 0
. 关键是0
。 If anything, use: 如有,请使用:
in_array('option_one', $_POST['options'])
array_key_exists looks for the keys of the arrays. array_key_exists查找数组的键。 You are looking for the value. 你正在寻找价值。 :) :)
What you want is 你想要的是什么
/**
* Check wheter parameter exists in $_POST['options'] and is not empty
*
* @param $needle
* @return boolean
*/
function in_options($needle)
{
if (empty($_POST['options']))
return false;
return in_array($needle, $_POST['options']);
}
Are you sure you are getting the correct information in your options[] array? 您确定在选项[]数组中获得了正确的信息吗? If you clicked option 1 and option 3, shouldn't the array look something like this? 如果单击选项1和选项3,那么数组看起来不应该是这样的吗?
[options] => Array
(
[0] => option_one
[1] => option_three
)
Since isset()
is a language construct and not a function , you may be able to rework the validation to be more efficient. 由于isset()
是一种语言结构而不是函数 ,因此您可以重新进行验证以提高效率。
function in_options($needle)
{
//Checking that it is set and not NULL is enough here.
if (! isset($_POST['options'][0]))
return false;
return in_array($needle, $_POST['options']);
}
The function empty()
works , but I think that $_POST['options'][0]
only needs to be checked for isset()
at this point . 该功能empty()
的作品 ,但我认为, $_POST['options'][0]
只需要检查isset()
在这一点上 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.