note 20144 modified in function.count by philip
| From: | philip@php.net | Date: | Fri, 13 Dec 2002 00:32:18 +0000 |
| Subject: | note 20144 modified in function.count by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40841@lists.php.net to get a copy of this message | ||
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.
--was--
Ah HA!... I told you i would do it and i have. Here it is... 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>
<head>
<title>Recursive Array Counting Function Test Page</title>
</head>
<body>
<?
$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);
?>
</body>
</html>
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.
Good Luck, and Best Regards,
~ Frank J. Mattia
CEO/Chief Engineer
Mattia & Harris Design Solutions ~
http://www.php.net/manual/en/function.count.php