无论大小写敏感,计算数组值的最简单方法是什么?试图$arr=array("A","B","b","A","b", "a", "A");print_r(array_count_values($arr));或者:$arr=array("AliCE","Alice","bob","AlICE","BOB", "aLIce", "alice");print_r(array_count_values($arr));演示电流输出Array( [A] => 3 [B] => 1 [b] => 2 [a] => 1)期望输出Array( [A] => 4 [B] => 2)或者:Array( [a] => 4 [b] => 2)
3 回答
PIPIONE
TA贡献1829条经验 获得超9个赞
我会使用array_map
但作为替代,加入一个字符串,更改大小写,拆分为一个数组:
print_r(array_count_values(str_split(strtolower(implode($arr)))));
动漫人物
TA贡献1815条经验 获得超10个赞
您可以先使用strtoupper以下方法将字母映射为大写:
$arr = array("A","B","b","A","b", "a", "A");
print_r(array_count_values(array_map('strtoupper', $arr)));
输出:
(
[A] => 4
[B] => 3
)
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
您可以使用foreach与array_key_exists和strtoupper
$arr=array("A","B","b","A","b", "a", "A");
$res = [];
foreach($arr as $k => $v){
array_key_exists(strtoupper($v), $res) ? ($res[strtoupper($v)]++) : ($res[strtoupper($v)] = 1);
}
print_r($res);
- 3 回答
- 0 关注
- 146 浏览
添加回答
举报
0/150
提交
取消