我创建了一个数组来获取文件,然后解析该文件的内容。我已经过滤掉了不足4个字符的字词if(strlen($value) < 4): unset($content[$key]); endif;
删除PHP数组中的选择字的有效方法
我的问题是这样的 - 我想从数组中删除常用单词,但其中有很多。我不知道是否有一种更有效的方法来做到这一点,而不是一遍又一遍地重复每个数组的值。
下面是我目前使用的代码示例。这个清单可能很大,我想这必须有一个更好(更有效率)的方式?
foreach ($content as $key=>$value) {
if(strlen($value) < 4): unset($content[$key]); endif;
if($value == 'that'): unset($content[$key]); endif;
if($value == 'have'): unset($content[$key]); endif;
if($value == 'with'): unset($content[$key]); endif;
if($value == 'this'): unset($content[$key]); endif;
if($value == 'your'): unset($content[$key]); endif;
if($value == 'will'): unset($content[$key]); endif;
if($value == 'they'): unset($content[$key]); endif;
if($value == 'from'): unset($content[$key]); endif;
if($value == 'when'): unset($content[$key]); endif;
if($value == 'then'): unset($content[$key]); endif;
if($value == 'than'): unset($content[$key]); endif;
if($value == 'into'): unset($content[$key]); endif;
}
上帝,我希望如此。 – 2012-08-04 22:57:13
至少,我建议在第一次检查后使用'elseif'进行检查。如果'$ value =='那'',则不需要继续检查其他可能的值。如答案中所示,有更好的方法来处理这个问题;我只想指出一些你可能能够应用到你写的其他代码的东西。 – 2012-08-04 23:31:15