我想检查字符串在比较2个不同的数组时是否包含相同的单词。如果每个数组中都有相同的单词它会显示多维数组
中每个数组中有多少个相同的单词数组一为普通数组类型,数组二为多维数组
数组1 :
Array
(
[0] => royalty
[1] => free
[2] => picture
)
数组2 :
Array
(
[0] => Array
(
[0] => Affordable and search from millions of royalty free picture
)
[1] => Array
(
[0] => from millions of royalty picture
)
[2] => Array
(
[0] => Provides free picture upload and hosting
)
[3] => Array
(
[0] => Post your picture here Get permanent links
)
[4] => Array
(
[0] => Choose your own unique username to access image
)
)
结果:Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 0
)
)
从上面的例子中,数组包含的每个单词的版税免费图片将显示在多维数组
中每个单词的相同数量我正在尝试使用strcasecmp(),但它只是给我0个结果,如果两个字符串包含相同的单词,我认为它不能给出正确的结果字符串与这么多的单词。
拆分字符串并查找与搜索词数组的交集
foreach($array2 as &$item)
$item[0] = count(array_intersect($array1, explode(' ', $item[0])));
最简单的方法可能是循环遍历数组2,并使用strstr()或preg_match
将每个单词与字符串进行比较像这样(不完整):
foreach($array2 as $sentence){
foreach($array1 as $word){
if(strstr($word, $sentence) !== false){
$wordsFound++;
}
}
}