note 37988 added to function.substr-count

From: Date: Wed, 03 Dec 2003 21:42:57 +0000
Subject: note 37988 added to function.substr-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-61366@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.substr-count.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37988 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37988&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37988&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#61366) next »