note 12166 deleted from function.count by googleguy
| From: | googleguy@php.net | Date: | Mon, 17 Sep 2012 00:11:31 +0000 |
| Subject: | note 12166 deleted from function.count by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190829@lists.php.net to get a copy of this message | ||
Note Submitter: kanareykin at denison dot edu
----
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;
}
}
?>