【发布时间】:2012-01-16 06:51:18
【问题描述】:
我正在解析一些文本并根据一些规则计算权重。所有角色的权重都相同。这会使 switch 语句很长我可以在 case 语句中使用范围吗?
我看到一个提倡关联数组的答案。
$weights = array(
[a-z][A-Z] => 10,
[0-9] => 100,
['+','-','/','*'] => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
foreach ($text as $character)
{
$total_weight += $weight[$character];
}
echo $weight;
实现这样的目标的最佳方法是什么? 有没有类似于 php 中的 bash case 语句的东西? 在关联数组或 switch 语句中写下每个单独的字符肯定不是最优雅的解决方案,还是唯一的选择?
【问题讨论】:
标签: php range switch-statement