3

ファイルをフェッチする配列を作成し、そのファイルの内容を解析しました。私はすでに4文字未満の単語を除外しましたif(strlen($value) < 4): unset($content[$key]); endif;

私の質問はこれです-私は配列から一般的な単語を削除したいのですが、それらのかなりの数があります。これらのチェックを各配列値に対して何度も繰り返す代わりに、これを行うためのより効率的な方法があるかどうか疑問に思いました。

これが私が現在使用しているコードのサンプルです。このリストは膨大になる可能性があり、より良い(より効率的な)方法が必要だと思いますか?

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;
}
4

7 に答える 7

2

多分これはより良いでしょう:

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

foreach ($content as $key=>$value) {
   if (in_array($value,$filter)){
      unset($content[$key])
   }
}
于 2012-08-04T23:01:58.563 に答える
2

これが私がそれをする方法です:

$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_replaceで、キーを最初の引数として渡し、配列自体を2番目の引数として渡します。この場合、結果は次のようになりますsome words to be replaced。このコードはテスト済みで、問題なく動作します。

配列を扱うときは、いくつかの奇抜な区切り文字(%@%@%または何かのようなもの)とstr_replaceたくさんを使って配列を分解し、もう一度たくさん爆発させて、ボブはあなたの叔父です


すべての単語を3文字未満に置き換えることになると(元の回答では忘れていました)、それは正規表現が得意なことです...私はpreg_replace('(\b|[^a-z])[a-z]{1,3}(\b|[^a-z])/i','$1$2',implode(',',$targetArray));そのようなことを言うでしょう。
これは私の頭のすぐ上にあり、テストされていないので、これをテストすることをお勧めします。しかし、これはあなたが始めるのに十分であるように思われるでしょう

于 2012-08-04T23:13:29.360 に答える
1

私はおそらくこのようなことをするでしょう:

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

foreach($content as $key => $value){
    if(in_array($value,$aCommonWords)){
        unset($content[$key]);
    }
}
于 2012-08-04T23:03:21.830 に答える
1

削除する単語の配列を作成し、値がその配列内にあるかどうかを確認します

$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];
于 2012-08-04T23:04:06.503 に答える
0

別の可能な解決策:

$arr = array_flip(array( 'that', 'have', 'with', 'this', 'your', 'will', 
        'they', 'from', 'when', 'then', 'than', 'into' ));
foreach ($content as $key=>$value) {
    if(strlen($value) < 4 || isset($arr[$value])) {
        unset($content[$key]);
    }
}
于 2012-08-04T23:13:05.483 に答える
0

使用array_diff()

$content = array('here','are','some','words','that','will','be','filtered');
$filter = array('that','have','here','are','will','they','from','when','then');
$result = array_diff($content, $filter);

結果:

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

または、フィルタリング対象の柔軟性を高めたい場合(たとえば、4文字未満の単語を除外する必要があると述べた場合)、次を使用できますarray_filter()

$result = array_filter($content, function($v) use ($filter) {
    return !in_array($v, $filter) && strlen($v) >= 4;
});

結果:

Array
(
    [2] => some
    [3] => words
    [7] => filtered
)
于 2018-08-17T01:46:29.830 に答える
0
$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

最も簡単な方法

于 2018-08-17T01:58:33.027 に答える