note 75737 modified in function.count by levim
| From: | levim@php.net | Date: | Thu, 10 Jan 2013 14:30:46 +0000 |
| Subject: | note 75737 modified in function.count by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192882@lists.php.net to get a copy of this message | ||
I actually find the following function more useful when it comes to multidimension arrays when you
do not want all levels of the array tree.
// $limit is set to the number of recursions
<?php
function count_recursive ($array, $limit) {
$count = 0;
foreach ($array as $id => $_array) {
if (is_array ($_array) && $limit > 0) {
$count += count_recursive ($_array, $limit - 1);
} else {
$count += 1;
}
}
return $count;
}
?>
--was--
I actually find the following function more useful when it comes to multidimension arrays when you
do not want all levels of the array tree.
// $limit is set to the number of recursions
<?php
function count_recursive ($array, $limit)
{
foreach ($array as $id => $_array)
{
if (is_array ($_array) && $limit > 0) $count += count_recursive ($_array, $limit - 1);
else $count += 1;
}
return $count;
}
?>
http://php.net/manual/en/function.count.php