在Php中,是否有任何方法可以计算一个数组有多少个键?
或适用于不带空的我在Php中,是否有任何方法可以计算一个数组有多少个键?,php,arrays,count,key,Php,Arrays,Count,Key,或适用于不带空的我 Array ( [0] => 'hello' [1] => 'there' [2] => [3] => [4] => 3 ) // how to get the number 5? 一个快捷菜单应该会调出所有可用功能的结果 输出: 内部(5) 下面的代码是用PHP5.3.2测试的。输出为int5 $a = array( 0 =>
Array
(
[0] => 'hello'
[1] => 'there'
[2] =>
[3] =>
[4] => 3
)
// how to get the number 5?
一个快捷菜单应该会调出所有可用功能的结果
输出:
内部(5)
下面的代码是用PHP5.3.2测试的。输出为
int5
$a = array(
0 => 'hello',
1 => 'there',
2 => null,
3 => null,
4 => 3,
);
var_dump(count($a));
你能提供更多关于null
未被计数的信息吗?也许是旧版本?或者干脆和我们其他人混在一起?:)
编辑:嗯,发布了错误的代码:)将忽略null;)那么为什么要使用echo计数(数组(1,null,null));给我3???
count(array_keys($arr))
那么可能吗?在示例中,即使键(2,3)为空,count仍会按它们存在的状态对它们进行计数count($arr)
即使存在false
、null
、0
、“
等,只要它们存在count()
就会将它们相加,正如MatTheCat
所说,回波计数(数组(1,null))代码>给出3我把自己的代码看错了,这让我觉得count忽略了空值。
Array
(
[0] => hello
[1] => there
[2] =>
[3] =>
[4] => 3
)
Count: 5
$arr = Array
(
0 => 'hello',
1 => 'there',
2 => null,
3 => null,
4 => 3,
);
var_dump(count($arr));
$a = array(
0 => 'hello',
1 => 'there',
2 => null,
3 => null,
4 => 3,
);
var_dump(count($a));
echo count($array);