note 37987 deleted from function.count-chars by tularis

From: Date: Mon, 27 Dec 2004 19:59:01 +0000
Subject: note 37987 deleted from function.count-chars by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-82362@lists.php.net to get a copy of this message
Note Submitter: bishop ---- Here is a simple item frequency counter function, useful in reporting or statistical apps: <?php function count_item(&$freq, $item, $inc = 1) { if (! is_array($freq)) { $freq = array (); } $freq[$item] = (isset($freq[$item]) ? ($freq[$item] += $inc) : $inc); return true; } ?> This function saves you the hassle of doing the isset() check each time you wish to increase the frequency. If you're not normally concerned over PHP's warnings about uninitialized variables, then you should probably start from the beginning. If you are, use the function above like: <?php count_item($freq, 'foo'); count_item($freq, 'bar'); count_item($freq, 'foo'); ?> After this, $freq will equal array ('foo' => 2, 'bar' => 1)

« previous php.notes (#82362) next »