使用 count() 函数可获取 PHP 数组的长度,该函数接受一个数组参数并返回数组中元素的数量。示例:$length = count($numbers);

如何获得 PHP 数组的长度
PHP 中获取数组长度的方法非常简单,可以使用 count() 函数。
使用方法:
<code class="php">$array_length = count($array);</code>
参数:
立即学习“PHP免费学习笔记(深入)”;
-
$array:要计算长度的数组
返回值:
- 返回数组中元素的数量(整数)
示例:
<code class="php">$numbers = [1, 2, 3, 4, 5]; $length = count($numbers); // $length 等于 5</code>
需要注意的是:
-
count()函数计算的是数组中元素的数量,包括空元素。 - 如果要计算数组中非空元素的数量,可以使用
array_filter()函数删除空元素,然后再使用count()函数计算。











