count() 函数返回数组元素个数
count — 计算数组中的单元数目或对象中的属性个数.count() 对没有初始化的变量返回 0,但对于空的数组也会返回 0.
例如一:
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a); /***$result == 3***/
$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count($b); /***$result == 3***/
$result = count(null); /***$result == 0***/
$result = count(false); /***$result == 1***/
?>
例如二:count() 的递归例子
<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard','pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); /***output 8***/
// normal count
echo count($food); /***output 2***/
?>
- 添加新评论
- 1048 次点击