Organizational Research By

Surprising Reserch Topic

Question:how to find counting the values with case-insensitive matching?



asked Sep 13, 2013 in PHP by ashish singh
edited Sep 12, 2013
0 votes
13 views



Related Hot Questions

1 Answer

0 votes

 

I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:
 

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);
?>
 
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.
answered Sep 13, 2013 by ashish singh
edited Sep 12, 2013

...