count
统计数组、Countable 对象中所有元素的数量
&reftitle.description;
intcount
Countablearrayvalue
intmodeCOUNT_NORMAL
用于数组时,统计数组中元素的数量;用于实现了 Countable
接口的对象时,返回 Countable::count 方法的返回值。
&reftitle.parameters;
value
数组或者 Countable 对象。
mode
如果可选的 mode 参数设为
COUNT_RECURSIVE(或 1),count
将递归地对数组计数。对计算多维数组的所有单元尤其有用。
count 能检测递归来避免无限循环,但每次出现时会产生 E_WARNING 错误
(如果 array 不止一次包含了自身)并返回大于预期的统计数字。
&reftitle.returnvalues;
返回 value 中的元素的数量。在 PHP 8.0.0
之前,如果参数既不是数组也不是实现了 Countable 接口的对象,将返回
1。当 value 为 &null; 时返回
0。
&reftitle.changelog;
&Version;
&Description;
8.0.0
当 value 参数传入了无效的 countable 类型,
count 现在会抛出 TypeError。
7.2.0
当 value 参数传入了无效的 countable 类型,
count 现在会产生警告。
&reftitle.examples;
count 例子
]]>
&example.outputs;
count 非 Countable|array 的例子 (这是个反例,请勿模仿)
]]>
&example.outputs;
&example.outputs.72;
&example.outputs.8;
递归 count 例子
array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));
// 递归计数
var_dump(count($food, COUNT_RECURSIVE));
// 常规计数
var_dump(count($food));
?>
]]>
&example.outputs;
Countable 对象
]]>
&example.outputs;
&reftitle.seealso;
is_array
isset
empty
strlen
is_countable
Array 数组