0

array ("things and stuff", "the stuff");各要素を別の配列の各要素と比較するために繰り返し処理していると仮定します-2番目の配列は("stuff the blah", "stuff and things to do");

そして、私は一般的に関連するものすべてにしたいと思います-位置が変更されていても、単語が同じである要素に一致する必要があります-基本的にこの場合は一致さpreg_matchせたいです。"the stuff""stuff the..."

これについて行くための最良の方法は何ですか?

4

1 に答える 1

0

最初の配列の文字列を単純に分割して、2 番目の配列のすべての文字列値と比較することができます。ここでは例を示します。

$array1 = array("things and stuff", "the stuff");
$array2 = array("stuff the blah", "stuff and things to do");

$result = array();

foreach($array1 as $val1){

    $words1 = explode(" ", $val1);

    $result[$val1] = 'not found';

    foreach($array2 as $val2){

        $words2 = explode(" ", $val2);
        $intersect = array_intersect($words1, $words2);
        $diff = array_diff($words1, $intersect);

        if(empty($diff))
            $result[$val1] = 'found';
    }
}

var_dump($result);

参考文献:

于 2012-11-24T06:48:36.940 に答える