Passing a null value as an array_keys parameter

The PHP documentation states that the default value for the second argument to array_keys is NULL.

However, when explicitly passing NULLs, array_keys functions do not work as expected.


Example:

code

$a = array(10=>'a', 11=>'b', 12=>'b', 13=>'c', 14=>'c', 15=>'b'); $keys = array_keys($a); var_dump($keys); //Output 0 $keys = array_keys($a, null); var_dump($keys); //Output 1 

Output

 array 0 => int 10 1 => int 11 2 => int 12 3 => int 13 4 => int 14 5 => int 15 array empty 

Question

I believe that it should look for keys whose value is null.

Passing a false or empty string results in exact behavior (obviously).

So what is the default value?


Answer

xdazz answer is correct . When checking the C code of this function, the first was mine, although it was a poor implementation at the C level (and was easily fixed by removing the code).

But then I realized that this is actually the intended behavior , since they went into a lot of problems so that you can test the NULL values ​​inside the array.

+4
source share
4 answers

The default value is hard to explain here.

This is a special case, the default value of the second parameter is actually not php NULL , but C NULL .

Immerse yourself in the source code:

 PHP_FUNCTION(array_keys) { zval *input, /* Input array */ *search_value = NULL, /* Value to search for */ //.... if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|zb", &input, &search_value, &strict) == FAILURE) { return; } // .... 

You could see that the default value is NULL for search_value , but if you specified the second parameter array_keys , then after zend_parse_parameters , search_value will not be NULL .

+4
source

I agree with xdazz. However, it seems that works 0. Don't ask me though! (And, YMMV.)

 $a = array(10=>'a', 'xaa'=>'b', 12=>'b', 13=>'c', 14=>'c', 15=>'b'); $keys = array_keys($a, 0); var_dump($keys); //Output 0 

Return:

 array(6) { [0]=> int(10) [1]=> string(3) "xaa" [2]=> int(12) [3]=> int(13) [4]=> int(14) [5]=> int(15) } 
+1
source

Since you are looking for zero, there is no result to give you an answer :)

0
source

It is difficult to conclude from the PHP manual regarding the use of the search_value parameter. I needed to try an example.

 If the optional search_value is specified, then only the keys for **that** value are returned. Otherwise, all the keys from the input are returned. 

This means that when search_value specified in array_keys() , values ​​in the array (not keys) are executed using search_value . If a match occurs, the key for this value is search_value in search_value .

Example

 <? $foo = array('first' => 'my_first', 'second' => 'my_second', 'third' => 'my_third', 'fourth' => null); $keys = array_keys($foo); var_dump($keys); //Output 0 $keys = array_keys($foo, 'my_second'); var_dump($keys); //Output 1 $keys = array_keys($foo, null); var_dump($keys); //Output 2 ?> 

Output

0

 array(4) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" [3]=> string(6) "fourth" } 

1

 array(1) { [0]=> string(6) "second" } 

2:

 array(1) { [0]=> string(6) "fourth" } 
0
source

All Articles