note 37987 added to function.count-chars
| From: | bishop at rn2 dot php dot net | Date: | Wed, 03 Dec 2003 21:41:16 +0000 |
| Subject: | note 37987 added to function.count-chars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61365@lists.php.net to get a copy of this message | ||
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)
----
Manual Page -- http://www.php.net/manual/en/function.count-chars.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37987
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37987&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37987&report=yes
Search -- http://master.php.net/manage/user-notes.php