Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP数组中删除选定单词的有效方法_Php_Arrays_Keyword - Fatal编程技术网

从PHP数组中删除选定单词的有效方法

从PHP数组中删除选定单词的有效方法,php,arrays,keyword,Php,Arrays,Keyword,我创建了一个数组来获取一个文件,然后解析该文件的内容。我已经用if(strlen($value)value的内容){ if(strlen($value)$value形式的内容){ if(strlen($value)

我创建了一个数组来获取一个文件,然后解析该文件的内容。我已经用
if(strlen($value)<4):unset($content[$key])过滤掉了少于4个字符的单词;endif

我的问题是-我想从数组中删除常用词,但是有很多常用词。我想知道是否有一种更有效的方法来实现这一点,而不是对每个数组值进行反复检查

下面是我目前使用的代码示例。这个列表可能会很大,我想必须有一个更好(更有效)的方法

foreach($key=>value的内容){
if(strlen($value)<4):未设置($content[$key]);endif;
if($value=='that'):未设置($content[$key]);endif;
if($value=='have'):未设置($content[$key]);endif;
if($value=='with'):未设置($content[$key]);endif;
if($value=='this'):未设置($content[$key]);endif;
if($value=='your'):未设置($content[$key]);endif;
if($value=='will'):未设置($content[$key]);endif;
if($value=='they'):unset($content[$key]);endif;
if($value=='from'):未设置($content[$key]);endif;
if($value=='when'):未设置($content[$key]);endif;
if($value=='then'):未设置($content[$key]);endif;
if($value=='than'):未设置($content[$key]);endif;
if($value=='into'):未设置($content[$key]);endif;
}

也许这样会更好:

$filter = array("that","have","with",...);

foreach ($content as $key=>$value) {
   if (in_array($value,$filter)){
      unset($content[$key])
   }
}

我可能会这样做:

$aCommonWords = array('that','have','with','this','yours','etc.....');

foreach($content as $key => $value){
    if(in_array($value,$aCommonWords)){
        unset($content[$key]);
    }
}

制作一个要删除的单词数组,并检查该值是否在该数组中

$exlcuded_words = array( 'that','have','with','this','your','will','they','from','when','then','than','into');
如果foreach

if (in_array($value, $excluded_words)) unset($content[$key];

另一种可能的解决办法:

$arr=array\u flip(数组('that'、'have'、'with'、'this'、'your'、'will'),
‘他们’、‘从’、‘何时’、‘然后’、‘比’、‘进入’);
foreach($key=>$value形式的内容){
if(strlen($value)<4 | | isset($arr[$value])){
未设置($content[$key]);
}
}
以下是我的做法:

$exlcuded_words = array( 'that','have','with','this','your','will','they','from','when','then','than','into');
$replace = array_fill_keys($exlcuded_words,'');
echo str_replace(array_keys($replace),$replace,'some words that have to be with this your will they have from when then that into replaced');
它的工作方式:创建一个充满空字符串的数组,其中键是要删除/替换的子字符串。只需使用
str\u replace
,将键作为第一个参数传递,将数组本身作为第二个参数传递,在本例中的结果是:
要替换的一些单词
。这段代码已经过测试,运行良好

处理数组时,只需使用一些古怪的分隔符(如
%@%@%
或其他东西)将其内爆,然后
str\u替换该批次,再次分解该批次,然后Bob的叔叔


当涉及到用少于3个字符替换所有单词时(我在原始答案中忘记了这一点),这是正则表达式所擅长的。。。我会说类似于
preg_replace('(\b|[^a-z])[a-z]{1,3}(\b|[^a-z])/I','1$2',内爆(',','targetaray')或类似的东西
您可能想测试一下这个,因为这只是我的想法,而且还没有测试过。但这似乎足以让您开始使用:

结果:

Array
(
    [2] => some
    [3] => words
    [6] => be
    [7] => filtered
)
Array
(
    [2] => some
    [3] => words
    [7] => filtered
)
或者,如果您希望在过滤内容方面有更大的灵活性(例如,您提到需要过滤少于4个字符的单词),您可以使用:

结果:

Array
(
    [2] => some
    [3] => words
    [6] => be
    [7] => filtered
)
Array
(
    [2] => some
    [3] => words
    [7] => filtered
)
结果:

abb
bffb
cbbb
dddd
f
g

最简单的方法

我会重新考虑删除所有少于3个字母的单词。如果使用缩写或缩略语而不是数组中的
,我认为最好使用
array\u key\u exists
isset($filter[$content])
。它比_array中的
更快
,并且结果几乎与最小值相同,我建议在第一次检查后的每次检查中使用
elseif
。如果
$value=='that'
,则无需继续检查其他可能的值。如答案所示,有更好的方法解决问题;我只是想指出一些你可以应用到你所写的其他代码中的东西。虽然这个代码片段可以解决这个问题,但它确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满你的代码,这会降低代码和解释的可读性!
$var = array('abb', 'bffb', 'cbbb', 'dddd', 'dddd', 'f', 'g');
$var= array_unique($var);
foreach($var as $val){
    echo $val. " ";
}
abb
bffb
cbbb
dddd
f
g