简体   繁体   English

为什么in_array()不适用于$ _POST?

[英]Why does in_array() not work on $_POST?

I'm trying to check that user's submitted data, from $_POST , has at least the same elements that my passed array has. 我正在尝试检查用户提交的数据,来自$_POST ,至少具有与我传递的数组相同的元素。 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). 我这样做是因为我稍后会通过调用$_POST['element']来使用这些元素,我不喜欢有关该元素的错误不存在(未设置)。 :) :)

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. 我不想使用像isset($_POST['x'], $_POST['y'], $_POST['z'])这样的东西isset($_POST['x'], $_POST['y'], $_POST['z'])因为每次我需要重写$_POST而且它似乎也不可读。

I tried to use in_array(array('x', 'y', 'z'), $_POST) , but it doesn't work (it returns false when it should return true ). 我试图用in_array(array('x', 'y', 'z'), $_POST)但它不工作(返回false时,它应该返回true )。 Any ideas how to make that work? 任何想法如何使这项工作? :) I'm sure that I have empty strings as $_POST['x'] , $_POST['y'] and $_POST['z'] . :)我确定我的空字符串为$_POST['x']$_POST['y']$_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. 我甚至试图将软管三个$_POST元素的值更改为空字符串以外的其他值 - 仍然...没有按预期工作。 :( :(

Thanks in an advice! 谢谢你的建议! :) :)

Edit: 编辑:

Just found out that in_array() checks values, not keys. 刚刚发现in_array()检查值,而不是键。 Then, I tried to do like this... 然后,我试着这样做......

in_array(array('title', 'slug', 'content'), array_keys($_POST))

Still, it returns false . 仍然,它返回false How does it comes so? 怎么会这样? ;/ ; /

Edit #2: 编辑#2:

Okay, here are results of debugging... 好的,这是调试结果......

Incoming $_POST : 传入$_POST

array(3) {
    ["title"]=>
    string(0) ""
    ["slug"]=>
    string(0) ""
    ["content"]=>
    string(0) ""
}

Result of array_keys($_POST) : 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)) : in_array(array('title', 'slug', 'content'), array_keys($_POST))

bool(false)

The question... why is it false ? 问题......为什么这是false I did all correct, as much as I know. 据我所知,我做的都是正确的。

Edit #3: 编辑#3:

At the end, I created my own method called Arr::keys_exists($keys, $array) . 最后,我创建了自己的方法,名为Arr::keys_exists($keys, $array)

in_array() checks to see if a value exists in an array, not a key . in_array()检查数组中是否存在 ,而不是 If you want to check to see if a key exists, then you'd want something like... 如果你想检查是否存在密钥,那么你需要像...那样的东西

in_array('x', array_keys($_POST));

or the simpler... 或者更简单......

array_key_exists('x', $_POST);

If you want to check for many keys at once: 如果要一次检查多个键:

$required_keys = array('x'=>1, 'y'=>1, 'z'=>1);
$missing_keys = array_diff_key($required_keys, $_POST);
$missing_keys_count = count($missing_keys);

Because in_array checks if the needle is in the array exactly. 因为in_array检查针是否在阵列中。 See example #3 of the manual-page. 请参见手册页的示例#3。 array_key_exists cannot work with a key as first argument because array's aren't valid with arrays as keys. array_key_exists不能使用键作为第一个参数,因为数组对于数组作为键无效。

You want something like all_in_array(array $needles, array $haystack); 你想要像all_in_array(array $needles, array $haystack); or array_all_keys_exists(array $keys, array $search); array_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(array('x', 'y', 'z'), $_POST), but it doesn't work (it returns false when it should return true) in_array(array('x','y','z'),$ _ POST),但它不起作用(当它返回true时返回false)

No, it shouldn't. 不,它不应该。 Read the manual of in_array . 阅读in_array的手册。

Checks if a value exists in an array 检查数组中是否存在

Instead you'd like to check array keys. 相反,你想检查数组键。 Get all the keys with array_keys and then use in_array. 使用array_keys获取所有键,然后使用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_array,您一次只能测试一个值,而不是像您尝试的那样完整的值数组。

In other words, if you do: 换句话说,如果你这样做:

in_array(array('title', 'slug', 'content'), array_keys($_POST))

It will to find one element of the keys array containing an array with title, slug and comment, which is not what you want. 它将找到keys数组的一个元素 ,其中包含一个带有title,slug和comment的数组,这不是你想要的。

First of all: 首先:

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. 我不想使用像isset这样的东西($ _ POST ['x'],$ _POST ['y'],$ _POST ['z'])因为每次我需要重写$ _POST而且它似乎也不可读。

You should never change one of the super globals ;) 你永远不应该改变其中一个超级全局变量;)

However, in_array() searches for values and not for keys 但是, in_array()搜索值而不搜索键

in_array(array('x', 'y', 'z'), array_key($_POST))
function getPost( $index, $default = '' )
{
    if ( isset( $_POST[ $index ] ) )
    {
        return $_POST[ $index ];
    }

    return $default;
}

If you want to ensure the presence of multiple keys, then array_diff might be applicable: 如果要确保存在多个密钥,则array_diff可能适用:

!array_diff(array('title', 'slug', 'content'), array_keys($_POST))

You might also be interested in array_intersect_uassoc . 您可能也对array_intersect_uassoc感兴趣。

@Eric是对的,试试这个 -

in_array(array('title', 'slug', 'content'), array(array_keys($_POST)))

You don't understand in_array . 你不明白in_array

$a = array(
    'x' => 1
);
echo in_array(array('x', 'y', 'z'), $a); // false

$a['an array'] = array('x', 'y', 'z');
echo in_array(array('x', 'y', 'z'), $a); // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM