note 19633 modified in function.count by philip
| From: | philip@php.net | Date: | Fri, 13 Dec 2002 00:29:06 +0000 |
| Subject: | note 19633 modified in function.count by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40836@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.
--was--
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