Edit report at https://bugs.php.net/bug.php?id=64854&edit=1

 ID:                 64854
 Comment by:         ni...@php.net
 Reported by:        bensor987 at neuf dot fr
 Summary:            array_key_exists( array('key1', 'key2', 'key3'),
                     $array);
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Arrays related
 Operating System:   All
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-05-16 11:23:15] 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 10:49:05] ni...@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 07:44:48] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64854&edit=1

Reply via email to