note 19633 added to function.count
| From: | frankm at mhdsolutions dot com | Date: | Wed, 06 Mar 2002 14:15:33 +0000 |
| Subject: | note 19633 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27413@lists.php.net to get a copy of this message | ||
Recently, while developing a portion of a script for my project at:
http://www.sourceforge.net/projects/htphp
I needed a way to count the number of values in an array. This shouldnt have been a problem except
the array was nested with multiple keys inside keys and i didnt want to include the keys in my
counting. Only the number of values, empty or otherwise. So, after much stress, i decided to write
this recursive function which will return the number of values in a multidimensional array.
/* ====================== */
function RCount($item, $key, $reset = FALSE) {
static $count = 0;
if($reset) { $count = 0; }
if(is_array($item)) {
array_walk($item, "RCount");
} else {
$count++;
}
return $count;
}
/* ====================== */
Its easy enough to use, just call:
$num = array_walk($this->_AttributesT, "RCount", TRUE);
Setting the third argument to true ensures that when you have a new array it doesnt add it on the
the result of the last call. Hope this helps anyone with a similar problem.
Best Regards,
~ Frank J. Mattia
CEO/Chief Engineer
Mattia & Harris Design Solutions
frankm at mhdsolutions dot com ~
--
http://www.php.net/manual/en/function.count.php
http://master.php.net/manage/user-notes.php?action=edit+19633
http://master.php.net/manage/user-notes.php?action=delete+19633
http://master.php.net/manage/user-notes.php?action=reject+19633