Why does in_array() not work on $_POST?
Asked Answered
B

8

5

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

Baseburner answered 8/10, 2011 at 16:37 Comment(3)
did you try to print_r($_POST) ?Yellowknife
See 'Edit #2' for results of debugging.Baseburner
in_array can only test a value at a time in_array('title', $array_keys) will work. see my updated answer.Yellowknife
C
10

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);
Carinacarinate answered 8/10, 2011 at 16:40 Comment(0)
D
1

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.

Divisibility answered 8/10, 2011 at 16:41 Comment(5)
So... I need to create my custom function that checks that keys are in 'master' array one by one, right?Baseburner
Yeah, if you think it'll make your program easier to understand/cleaner than using isset($_POST['key1'], $_POST['key2'], etc...);Divisibility
Take a look at 'Edit #3'. I created my own method for it. :)Baseburner
Looks fine. You don't have to use strict === checking, because array_key_exists only return true or false. But there's noting wrong with it.Divisibility
I'm using strict type-checking only because that function returns only boolean. Looks more clearer to me! :)Baseburner
Y
0

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.

Yellowknife answered 8/10, 2011 at 16:40 Comment(0)
A
0

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))
Acquaint answered 8/10, 2011 at 16:41 Comment(0)
J
0
function getPost( $index, $default = '' )
{
    if ( isset( $_POST[ $index ] ) )
    {
        return $_POST[ $index ];
    }

    return $default;
}
Jake answered 8/10, 2011 at 16:44 Comment(0)
E
0

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.

Eric answered 8/10, 2011 at 16:45 Comment(0)
C
0

@Eric was right, try this -

in_array(array('title', 'slug', 'content'), array(array_keys($_POST)))
Curative answered 8/10, 2011 at 17:0 Comment(0)
S
-1

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
Strophic answered 8/10, 2011 at 16:46 Comment(1)
That was the first thing I thought when I noticed that my code doesn't work as expected. This means that I need to create my custom method for it.Baseburner

© 2022 - 2024 — McMap. All rights reserved.