PHP count() 函數(shù)
PHP count() 函數(shù)
實(shí)例
返回?cái)?shù)組中元素的數(shù)目:
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
運(yùn)行實(shí)例 ?
定義和用法
count() 函數(shù)返回?cái)?shù)組中元素的數(shù)目。
語(yǔ)法
count(array,mode);
參數(shù) | 描述 |
---|---|
array | 必需。規(guī)定要計(jì)數(shù)的數(shù)組。 |
mode | 可選。規(guī)定函數(shù)的模式??赡艿闹担?ul>
|
技術(shù)細(xì)節(jié)
返回值: | 返回?cái)?shù)組中元素的數(shù)目。 |
---|---|
PHP 版本: | 4+ |
更新日志: | mode 參數(shù)是在 PHP 4.2 中新增的。 |
更多實(shí)例
實(shí)例 1
遞歸地計(jì)算數(shù)組中元素的數(shù)目:
<?php
$cars=array
(
"Volvo"=>array
(
"XC60",
"XC90"
),
"BMW"=>array
(
"X3",
"X5"
),
"Toyota"=>array
(
"Highlander"
)
);
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
$cars=array
(
"Volvo"=>array
(
"XC60",
"XC90"
),
"BMW"=>array
(
"X3",
"X5"
),
"Toyota"=>array
(
"Highlander"
)
);
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
運(yùn)行實(shí)例 ?
