6.5.5 PHP数组中统计数组元素的个数与唯一性的函数
统计数组元素的个数与唯一性
count -- 计算数组中的单元数目或对象中的属性个数
语法:int count ( mixed var [, int mode] )
如果可选的 mode 参数设为 COUNT_RECURSIVE(或 1),count() 将递归地对数组计数。
array_count_values -- 统计数组中所有的值出现的次数
语法:array array_count_values ( array input )
array_unique -- 移除数组中重复的值
语法:array array_unique ( array array )
例1.php
<?php
$lamp = array("os"=>"Linux", "webserver"=>"Apache","db"=>"MySQL", "language"=>"PHP");
print_r($lamp);
echo '<br>';
$str = "";
var_dump(count($str)); //strlen();
例2.php
<?php
/* $lamp = array("os"=>"Linux", "webserver"=>"Apache","db"=>"MySQL", "language"=>"PHP");
print_r($lamp);
echo '<br>';
*/
$web = array(
"lamp"=>array("os"=>"Linux", "webserver"=>"Apache","db"=>"MySQL", "language"=>"PHP"),
"JavaEE" => array("os"=>"Unix", "webserver"=>"Tomcat","db"=>"Oracle", "language"=>"JSP")
);
echo count($web, 1);
例 3 test.php
<?php
// $lamp = array("os"=>"Linux", "webserver"=>"Apache","db"=>"MySQL", "language"=>"PHP");
print_r($lamp);
echo '<br>';