4

PHP 文档指出 array_keys 第二个参数的默认值为NULL

但是,当显式传递NULL时,array_keys 似乎无法正常工作。


例子:

代码

$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

输出

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

array
  empty

问题

我认为它必须试图找到值为空的键。

传递 false 或空字符串会产生完全相同的行为(显然)。

那么,默认值是多少?


回答

xdazz 的答案是正确的。在检查这个函数的 C 代码时,我的第一个想法是这是一个糟糕的 C 级别实现(通过删除代码很容易修复)

But then I realized this is actually the intended behavior since they went to a lot of trouble to enable you to test for NULL values inside the array.

4

4 回答 4

4

The default value is hard to explain here.

It is special case, the default value of second parameter is not actually the php's NULL, but the C level's NULL.

Dive into 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, the default value is NULL for search_value, but if you specified the second parameter of array_keys, then after zend_parse_parameters, search_value will not be NULL.

于 2012-12-16T11:14:39.633 回答
1

I agree with xdazz. However, it seems that passing 0 works. Don't ask me how, 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

Returns:

array(6) { [0]=> int(10) [1]=> string(3) "xaa" [2]=> int(12) [3]=> int(13) [4]=> int(14) [5]=> int(15) } 
于 2012-12-16T11:16:57.510 回答
0

Since you're searching for null, there's no result to give back to you :)

于 2012-12-16T10:54:53.700 回答
0

It is hard to infer from PHP manual as to the use of search_value parameter. I needed to do 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.

It means that when search_value is specified in array_keys(), the values in the array (not keys) are searched against search_value. If match occurs, the key for that value in search_value is returned.

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"
}
于 2012-12-16T11:11:58.497 回答