【发布时间】:2011-05-05 14:51:49
【问题描述】:
Array
(
[0] => 'hello'
[1] => 'there'
[2] =>
[3] =>
[4] => 3
)
// how to get the number 5?
【问题讨论】:
-
我看错了自己的代码,这让我认为 count 忽略了 null 值。
Array
(
[0] => 'hello'
[1] => 'there'
[2] =>
[3] =>
[4] => 3
)
// how to get the number 5?
【问题讨论】:
$arr = Array
(
0 => 'hello',
1 => 'there',
2 => null,
3 => null,
4 => 3,
);
var_dump(count($arr));
输出:
int(5)
【讨论】:
count(array_keys($arr)) 那么也许?
count($arr) 即使有 false,null,0,"" 等,只要它们存在 count() 就会将它们加起来,正如 MatTheCat 所说,echo count(array(1,null,null)); 给出 3
echo count($array);
【讨论】:
以下代码使用 PHP 5.3.2 进行了测试。输出为int 5。
$a = array(
0 => 'hello',
1 => 'there',
2 => null,
3 => null,
4 => 3,
);
var_dump(count($a));
您能否提供更多关于null 未被统计的信息?可能是旧版本?或者只是在和我们其他人混在一起? :)
编辑:好吧,发布了错误的代码:)
【讨论】:
为我工作,带 NULL
$array = array('hello', 'there', NULL, NULL, 3);
echo "<pre>".print_r($array, true)."</pre><br />";
echo "Count: ".count($array)."<br />";
输出
Array
(
[0] => hello
[1] => there
[2] =>
[3] =>
[4] => 3
)
Count: 5
快速Google search for PHP Array 应该会提取所有可用功能的结果
【讨论】: