note 100064 deleted from function.count by googleguy
| From: | googleguy@php.net | Date: | Mon, 17 Sep 2012 00:08:47 +0000 |
| Subject: | note 100064 deleted from function.count by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190824@lists.php.net to get a copy of this message | ||
Note Submitter: marc at example dot com
----
Just found use for this function...
Can count an array of items where a certain condition is met.
Feel free to improve it.
function countWhere($input = 1, $operator = '==', $where = 1)
{
$input = is_array($input) ? $input : (array)$input;
$operator = !in_array($operator, array('<','>','<=',
'>=','==', '!=')) ? '==' : $operator;
$i = 0;
foreach($input as $current)
{
$match = null;
eval('$match =
(bool)("'.$current.'"'.$operator.'"'.$where.'");');
$i = $match ? $i+1 : $i;
}
return $i;
}
USAGE:
echo countWhere(array('PZ','AB','CD','EF','GH'),
'!=', 'GH');
OUTPUT:
4