note 20144 added to function.count

From: Date: Sun, 24 Mar 2002 07:14:40 +0000
Subject: note 20144 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-28139@lists.php.net to get a copy of this message
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 http://master.php.net/manage/user-notes.php?action=edit+20144 http://master.php.net/manage/user-notes.php?action=delete+20144 http://master.php.net/manage/user-notes.php?action=reject+20144

« previous php.notes (#28139) next »