为什么 in_array() 对 $_POST 不起作用?

发布于 2024-12-08 17:14:11 字数 1576 浏览 0 评论 0原文

我正在尝试检查用户从 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

双马尾 2024-12-15 17:14:12

in_array() 检查数组中是否存在,而不是。如果您想检查某个密钥是否存在,那么您需要类似...

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

或更简单的...

array_key_exists('x', $_POST);

如果您想一次检查多个密钥:

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

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...

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);
后来的我们 2024-12-15 17:14:12

因为 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); or 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.

和影子一齐双人舞 2024-12-15 17:14:12

首先:

我不想使用像 isset($_POST['x'], $_POST['y'], $_POST['z']) 这样的东西,因为每次我都需要重写 $_POST 并且看起来也无法阅读。

您永远不应该更改超级全局变量之一;)

但是,in_array() 搜索值而不是键

in_array(array('x', 'y', 'z'), array_key($_POST))

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.

You should never change one of the super globals ;)

However, in_array() searches for values and not for keys

in_array(array('x', 'y', 'z'), array_key($_POST))
你与昨日 2024-12-15 17:14:12
function getPost( $index, $default = '' )
{
    if ( isset( $_POST[ $index ] ) )
    {
        return $_POST[ $index ];
    }

    return $default;
}
function getPost( $index, $default = '' )
{
    if ( isset( $_POST[ $index ] ) )
    {
        return $_POST[ $index ];
    }

    return $default;
}
夜光 2024-12-15 17:14:12

如果您想确保存在多个键,则 array_diff 可能适用

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

:可能还对 array_intersect_uassoc 感兴趣。

If you want to ensure the presence of multiple keys, then array_diff might be applicable:

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

You might also be interested in array_intersect_uassoc.

旧竹 2024-12-15 17:14:12

in_array(array('x', 'y', 'z'), $_POST),但它不起作用(当它应该返回 true 时却返回 false)

不,它不应该。阅读in_array的手册。

检查数组中是否存在

相反,您想检查数组键。使用 array_keys 获取所有键,然后使用 in_array。

不过,使用 in_array 一次只能测试一个值,而不是像您尝试做的那样测试整个值数组。

换句话说,如果你这样做:

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

它将找到keys数组的一个元素,其中包含一个带有标题、slug和comment的数组,这不是你想要的。

in_array(array('x', 'y', 'z'), $_POST), but it doesn't work (it returns false when it should return true)

No, it shouldn't. Read the manual of 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.

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:

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.

你怎么敢 2024-12-15 17:14:12

@Eric 是对的,试试这个 -

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

@Eric was right, try this -

in_array(array('title', 'slug', 'content'), array(array_keys($_POST)))
演出会有结束 2024-12-15 17:14:12

您不理解 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

You don't understand 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文