无论如何要计算一个数组在Php中有多少键?

Moh*_*mad 5 php arrays key count

Array
    (
        [0] => 'hello'
        [1] => 'there'
        [2] => 
        [3] => 
        [4] => 3
    )

// how to  get the number 5?
Run Code Online (Sandbox Code Playgroud)

tee*_*one 21

计数

$arr = Array
    (
        0 => 'hello',
        1 => 'there',
        2 => null,
        3 => null,
        4 => 3,
    );
var_dump(count($arr));
Run Code Online (Sandbox Code Playgroud)

输出:

INT(5)

  • `count(array_keys($ arr))`那么也许呢? (3认同)
  • 在该示例中,即使键(2,3)为空,计数仍会在它们存在时计数.`count($ arr)`即使有`false`,`null`,`0`,``"`等等,只要它们存在`count()`就会把它们加起来,就像`MatTheCat`所说的那样,`echo count(array(1,null,null));`给出3 (3认同)