在PHP中,数组函数 count () 用来计算数组中的元素数目。
函数语法:
count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] ) : int
函数参数说明:
参数 | 描述 |
---|---|
array | 必需。规定要计数的数组。 |
mode | 可选。规定函数的模式。可能的值:
|
count () 函数用来计算数组中的元素数目。如果参数既不是数组,也不是实现 Countable 接口的对象,将返回 1。 如果 array_or_countable 是 NULL 则结果是 0。
举例1,计算数组中的元素数目:
<?php // 定义数组 $arr = array('b' => 'banana', 'a' => 'apple', 'g' => 'green orange', 'o' => 'orange'); var_dump(count($arr));
以上代码输出如下:
int 4