php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64854 array_key_exists( array('key1', 'key2', 'key3'), $array);
Submitted: 2013-05-16 07:44 UTC Modified: 2013-05-16 10:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: bensor987 at neuf dot fr Assigned:
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-05-16 07:44 UTC] bensor987 at neuf dot fr
Description:
------------
Why can't we give an array as the first parameter of array_key_exists() ? I have a few cases where it would be useful. Especially when checking $_POST, $_GET or $_REQUEST arrays (for instance).

Test script:
---------------
$array_to_test = array( 'key1' => 1, 'key2' => 1, 'key3' => 1 );
$array_keys = array( 'key1', 'key2', 'key3');
			
var_dump( array_key_exists( $array_keys, $array_to_test ) );
var_dump( (array_key_exists( 'key1', $array_to_test ) && array_key_exists( 'key2', $array_to_test ) && array_key_exists( 'key3', $array_to_test )) );// The same as above, but much longer.

Expected result:
----------------
bool(true) bool(true)

Actual result:
--------------
bool(false) bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-16 10:49 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-05-16 10:49 UTC] nikic@php.net
For "normal" arrays (with no odd null-value usages), you can just use isset for this. E.g. isset($_GET['foo'], $_GET['bar'], $_GET['baz']).

I think accepting an array for array_key_exists is not very clear, because it could mean either "one of the keys exists" or "all of the keys exist".

Marking as Wfx, unless some clearer examples for use cases come up ;)
 [2013-05-16 11:23 UTC] bensor987 at neuf dot fr
isset() doesn't behave like array_key_exists(). It will sometimes return false when the key exists. I want the result to be as realist as possible.

Example :
$array = array( 'key_null' => NULL );

var_dump( isset( $array['key_null'] ) );
var_dump( array_key_exists( 'key_null', $array ) );
			

Output : bool(false) bool(true).
 [2013-05-16 11:44 UTC] nikic@php.net
Sure, that's why I said that it only applies to arrays "with no odd null-value usages". You used $_GET and $_POST as examples in your FR, both of which can only contain string and array values (no nulls). Thus for them multi-parameter isset() is sufficient. That's why I asked whether you have any other particular use cases in mind.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC