note 37988 deleted from function.substr-count by nlopess

From: Date: Tue, 12 Jul 2005 15:12:42 +0000
Subject: note 37988 deleted from function.substr-count by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-91817@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 (#91817) next »