note 20144 rejected from function.count by didou
| From: | didou@php.net | Date: | Sun, 26 Oct 2003 15:41:31 +0000 |
| Subject: | note 20144 rejected from function.count by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59325@lists.php.net to get a copy of this message | ||
Note Submitter: frankm@mhdsolutions.com
----
This function will count all elements, in a multidemensional array with keys. The function will
return a count of all the values in a keyed array with multiple dimensions. The example here shows
you a sample array and a count of all the items in it. The reset argument does not reset the
internal array pointer location. What it does is set the count variable back to zero whenever you
want to count a new array... After all, it is static.
Heres the code.
<html>
<?php
$rarray = array(
"1" => array(
"1.1" => array(
"1.1.1",
"1.1.2",
"1.1.3"
),
"1.2",
"1.3"
)
);
function Array_RCount($array, $reset = FALSE) {
static $count;
$hKeys = array();
if($reset)
$count = 0;
is_array($array) or die("First argument of Array_RCount() should be an array");
$hKeys = array_keys($array);
if(sizeof($hKeys) == 0)
return 0;
for($i = 0; $i < sizeof($hKeys); $i++) {
if(is_array($array[$hKeys[$i]])) {
Array_RCount($array[$hKeys[$i]]);
} else {
$count++;
}
}
return $count;
}
echo Array_RCount($rarray, TRUE);
?>
If anyone can find a bug please comment on it and ill try to fix it. Also, **and i hope my comment
doesnt get removed for this =) ** i think that the PHP team should add this to the default
functionality of php. Someday when i get a chance i may add the function then send them the diffs.
Who knows.