note 73389 added to function.array-count-values
| From: | rabies dot dostojevski at gmail dot com | Date: | Wed, 21 Feb 2007 09:40:15 +0000 |
| Subject: | note 73389 added to function.array-count-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123626@lists.php.net to get a copy of this message | ||
I couldn't find a function for counting the values with case-insensitive matching, so I wrote a
quick and dirty solution myself:
<pre><?php
function array_icount_values($array) {
$ret_array = array();
foreach($array as $value) {
foreach($ret_array as $key2 => $value2) {
if(strtolower($key2) == strtolower($value)) {
$ret_array[$key2]++;
continue 2;
}
}
$ret_array[$value] = 1;
}
return $ret_array;
}
$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J.
Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY',
'Fastway', 'fastway', 'YUP');
$ar2 = array_count_values($ar); // Normal matching
$ar = array_icount_values($ar); // Case-insensitive matching
print_r($ar2);
print_r($ar);
?></pre>
This prints:
Array
(
[J. Karjalainen] => 3
[60] => 2
[j. karjalainen] => 1
[Fastway] => 2
[FASTWAY] => 1
[fastway] => 1
[YUP] => 1
)
Array
(
[J. Karjalainen] => 4
[60] => 2
[Fastway] => 4
[YUP] => 1
)
I don't know how efficient it is, but it seems to work. Needed this function in one of my
scripts and thought I would share it.
----
Server IP: 62.237.7.3
Probable Submitter: 84.231.148.57
----
Manual Page -- http://www.php.net/manual/en/function.array-count-values.php
Edit -- https://master.php.net/note/edit/73389
Del: integrated -- https://master.php.net/note/delete/73389/integrated
Del: useless -- https://master.php.net/note/delete/73389/useless
Del: bad code -- https://master.php.net/note/delete/73389/bad+code
Del: spam -- https://master.php.net/note/delete/73389/spam
Del: non-english -- https://master.php.net/note/delete/73389/non-english
Del: in docs -- https://master.php.net/note/delete/73389/in+docs
Del: other reasons-- https://master.php.net/note/delete/73389
Reject -- https://master.php.net/note/reject/73389
Search -- https://master.php.net/manage/user-notes.php