count — 計算數組中的單元數目或對象中的屬性個數
strlen是統計數組中元素的長度;
你如果想統計數組中所有元素的長度,那就用循環統計吧
代碼:
$a = array('0' => '123', '1' => 'asdf');
$count = count($a);
$a_length = 0;
for ($i=0; $i<$count; $i++) {
$a_length = $a_length + strlen($a[$i]);
}
print $a_length;
代碼:
<?phptqeb
$a[0]
?>
Example #1 count() 例子
<?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
?>
Example #2 遞歸 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
?>
strlen是統計數組中元素的長度;
你如果想統計數組中所有元素的長度,那就用循環統計吧
代碼:
$a = array('0' => '123', '1' => 'asdf');
$count = count($a);
$a_length = 0;
for ($i=0; $i<$count; $i++) {
$a_length = $a_length + strlen($a[$i]);
}
print $a_length;
代碼:
<?phptqeb
$a[0]
?>
Example #1 count() 例子
<?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
?>
Example #2 遞歸 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
?>