php数组长度函数的例子
PHP中有两个内建的函数:count() 和sizeof(),可以实现得到数组的长度:
- $colorList = array("apple"=>"red", "grass"=>"green", "sky"=>"blue", "night"=>"black","wall"=>"white");
- echo "Array size with count: ".count($colorList);
- echo "Array size with count: ".sizeof($colorList);
例子:
- $arr = Array('0','1','2','3','4');
- echo count($arr);
- // 输出 5
- $arr = array('A','B','C');
- echo sizeof($arr);
- //输出3
二维数组:
count函数有两个参数:
0(或COUNT_NORMAL)为默认,不检测多维数组(数组中的数组);
1(或COUNT_RECURSIVE)为检测多维数组,
你可以使用这样的代码来测试该函数:
- <?php
- $arr=array(
- 0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
- 1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
- );
- echo '不统计多维数组:'.count($arr,0);//count($arr,COUNT_NORMAL)
- echo "<br/>";
- echo '统计多维数组:'.count($arr,1);//count($arr,COUNT_RECURSIVE)
- ?>