note 12166 modified in function.count by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:55:42 +0000 |
| Subject: | note 12166 modified in function.count by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79961@lists.php.net to get a copy of this message | ||
Here's how to count non-empty elements
in an array of any dimension. Hope
it will be useful for somebody.
<?php
// recursively count all non-empty elements
// in array of any dimension or mixed - i.e.
// array('1' => 2, '2' => array('1' => 3, '2' =>
4))
function count_all($arg)
{
// skip if argument is empty
if ($arg) {
// not an array, return 1 (base case)
if(!is_array($arg))
return 1;
// else call recursively for all elements $arg
foreach($arg as $key => $val)
$count += count_all($val);
return $count;
}
}
?>
--was--
Here's how to count non-empty elements
in an array of any dimension. Hope
it will be useful for somebody.
// recursively count all non-empty elements
// in array of any dimension or mixed - i.e.
// array('1' => 2, '2' => array('1' => 3, '2' =>
4))
function count_all($arg)
{
// skip if argument is empty
if ($arg) {
// not an array, return 1 (base case)
if(!is_array($arg))
return 1;
// else call recursively for all elements $arg
foreach($arg as $key => $val)
$count += count_all($val);
return $count;
}
}
http://php.net/manual/en/function.count.php