count :
Count array elements.
Syntax :
int count ( mixed var [, int mode])
Returns the number of elements in var, which is typically an array.
If var is not an array, 1 will be returned .
If the optional mode parameter is set to COUNT_RECURSIVE , count() will recursively count the array.
Example 1:
<?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;
?>
$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;
?>
Example 2:
<?php
$food = array('fruits'
=> array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
// count
echo count($food); // output 2
?>
'veggie' => array('carrot', 'collard', 'pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
// count
echo count($food); // output 2
?>
Comments
Post a Comment