为什么 in_array() 对 $_POST 不起作用?
我正在尝试检查用户从 $_POST
提交的数据是否至少具有与我传递的数组相同的元素。我这样做是因为稍后我将通过调用 $_POST['element']
使用这些元素,并且我不喜欢有关该元素不存在(未设置)的错误。 :)
我不想使用像 isset($_POST['x'], $_POST['y'], $_POST['z'])
这样的东西,因为每次我都需要重写$_POST
,它似乎也不可读。
我尝试使用 in_array(array('x', 'y', 'z'), $_POST)
,但它不起作用(当它返回 false
它应该返回true
)。有什么想法可以让它发挥作用吗? :) 我确信我有空字符串 $_POST['x']
、$_POST['y']
和 $_POST['z ']
。我什至尝试将软管三个 $_POST
元素的值更改为除空字符串之外的其他内容 - 仍然......无法按预期工作。 :(
感谢您的建议!:)
编辑:
刚刚发现 in_array()
检查值,而不是键。然后,我尝试这样做...
in_array(array('title', 'slug', 'content'), array_keys($_POST))
尽管如此,它还是返回 false< /代码>。怎么会这样呢? ;/
编辑 #2:
好的,这是调试结果...
传入 $_POST
:
array(3) {
["title"]=>
string(0) ""
["slug"]=>
string(0) ""
["content"]=>
string(0) ""
}
array_keys($_POST)
的结果:
array(3) {
[0]=>
string(5) "title"
[1]=>
string(4) "slug"
[2]=>
string(7) "content"
}
结果in_array(array('title', 'slug', 'content'), array_keys($_POST))
:
bool(false)
问题...为什么是false
?据我所知,我都做对了。
编辑#3:
最后,我创建了自己的方法,名为 Arr:: key_exists($keys, $array)
。
I'm trying to check that user's submitted data, from $_POST
, has at least the same elements that my passed array has. I'm doing it because I will use those elements later by calling $_POST['element']
and I don't like errors about that element doesn't exist (isn't set). :)
I don't want to use something like isset($_POST['x'], $_POST['y'], $_POST['z'])
because each time I need to rewrite $_POST
and it seems unreadable as well.
I tried to use in_array(array('x', 'y', 'z'), $_POST)
, but it doesn't work (it returns false
when it should return true
). Any ideas how to make that work? :) I'm sure that I have empty strings as $_POST['x']
, $_POST['y']
and $_POST['z']
. I even tried to change values of hose three $_POST
elements to something other than empty string - still... doesn'y work as expected. :(
Thanks in an advice! :)
Edit:
Just found out that in_array()
checks values, not keys. Then, I tried to do like this...
in_array(array('title', 'slug', 'content'), array_keys($_POST))
Still, it returns false
. How does it comes so? ;/
Edit #2:
Okay, here are results of debugging...
Incoming $_POST
:
array(3) {
["title"]=>
string(0) ""
["slug"]=>
string(0) ""
["content"]=>
string(0) ""
}
Result of array_keys($_POST)
:
array(3) {
[0]=>
string(5) "title"
[1]=>
string(4) "slug"
[2]=>
string(7) "content"
}
Result of in_array(array('title', 'slug', 'content'), array_keys($_POST))
:
bool(false)
The question... why is it false
? I did all correct, as much as I know.
Edit #3:
At the end, I created my own method called Arr::keys_exists($keys, $array)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
in_array()
检查数组中是否存在值,而不是键。如果您想检查某个密钥是否存在,那么您需要类似...或更简单的...
如果您想一次检查多个密钥:
in_array()
checks to see if a value exists in an array, not a key. If you want to check to see if a key exists, then you'd want something like...or the simpler...
If you want to check for many keys at once:
因为
in_array
检查针是否在确切地说是数组。请参阅手册页的示例#3。array_key_exists
无法使用键作为第一个参数,因为数组作为键无效。您想要类似
all_in_array(array $needles, array $haystack);
或array_all_keys_exists(array $keys, array $search);
的内容,它返回所有元素是否都在数组中。您可能可以自己实现类似的操作,或者在此处寻求更多帮助。Because
in_array
checks if the needle is in the array exactly. See example #3 of the manual-page.array_key_exists
cannot work with a key as first argument because array's aren't valid with arrays as keys.You want something like
all_in_array(array $needles, array $haystack);
orarray_all_keys_exists(array $keys, array $search);
which returns whether all elements are in the array. You can probably implement something like this yourself, or ask for more help here.首先:
您永远不应该更改超级全局变量之一;)
但是,
in_array()
搜索值而不是键First of all:
You should never change one of the super globals ;)
However,
in_array()
searches for values and not for keys如果您想确保存在多个键,则
array_diff
可能适用:可能还对
array_intersect_uassoc
感兴趣。If you want to ensure the presence of multiple keys, then
array_diff
might be applicable:You might also be interested in
array_intersect_uassoc
.不,它不应该。阅读in_array的手册。
相反,您想检查数组键。使用 array_keys 获取所有键,然后使用 in_array。
不过,使用 in_array 一次只能测试一个值,而不是像您尝试做的那样测试整个值数组。
换句话说,如果你这样做:
它将找到keys数组的一个元素,其中包含一个带有标题、slug和comment的数组,这不是你想要的。
No, it shouldn't. Read the manual of in_array.
Instead you'd like to check array keys. Get all the keys with array_keys and then use in_array.
With in_array you can test only one value at a time, though, not a whole array of values like you're trying to do.
In other words, if you do:
It will to find one element of the keys array containing an array with title, slug and comment, which is not what you want.
@Eric 是对的,试试这个 -
@Eric was right, try this -
您不理解
in_array
。You don't understand
in_array
.