PHP将所有数组值搜索到另一个每个数组值中
好的,这是我想做的 我有一个包含关键字的数组PHP将所有数组值搜索到另一个每个数组值中,php,arrays,Php,Arrays,好的,这是我想做的 我有一个包含关键字的数组 $keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player'); 我还有另一个数组,它包含了我的全部标题 比方说 $titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a r
$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
我还有另一个数组,它包含了我的全部标题
比方说
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.','Cristiano Ronaldo is World Soccer's Player of the Year 2013.','Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona','','');
那么现在我想做什么呢
是在每个titles数组元素中循环我的关键字数组
如果一个元素中有3个关键字,那么做些什么
比如说
$titiles[0] // this one has these words => Madrid , cup club
所以这一个是有至少3个字的我的关键字
因此,如果每个元素有3个或更多关键字,则回显该数组元素
你知道怎么让它工作吗
foreach ($titles as $t){
$te=explode(' ',$t);
$c=count(array_intersect($te,$keywords));
if($c >=3){
echo $t.' has 3 or more matches';
}
}
现场演示:
2个匹配项是您当前的最大匹配项
如果你想马德里与马德里
$keywords=array_map('strtolower', $keywords);
foreach ($titles as $t){
$te=explode(' ',$t);
$comp=array_map('strtolower', $te);
$c=count(array_intersect($comp,$keywords));
if($c >=3){
echo $t.' has 3 or more matches';
}
}
现场演示:
2个匹配项是您当前的最大匹配项
如果你想马德里与马德里
$keywords=array_map('strtolower', $keywords);
foreach ($titles as $t){
$te=explode(' ',$t);
$comp=array_map('strtolower', $te);
$c=count(array_intersect($comp,$keywords));
if($c >=3){
echo $t.' has 3 or more matches';
}
}
或者,您也可以使用
substr\u count()
获取发生次数。考虑这个例子:
$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.',"Cristiano Ronaldo is World Soccer's Player of the Year 2013.","Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona",'','');
$count = 0;
$data = array();
foreach($titles as $key => $value) {
$value = strtolower($value);
$keys = array_map('strtolower', $keywords);
foreach($keys as $needle) {
$count+= substr_count($value, $needle);
}
echo "In title[$key], the number of occurences using keywords = " .$count . '<br/>';
$count = 0;
}
或者,您也可以使用
substr\u count()
获取发生次数。考虑这个例子:
$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.',"Cristiano Ronaldo is World Soccer's Player of the Year 2013.","Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona",'','');
$count = 0;
$data = array();
foreach($titles as $key => $value) {
$value = strtolower($value);
$keys = array_map('strtolower', $keywords);
foreach($keys as $needle) {
$count+= substr_count($value, $needle);
}
echo "In title[$key], the number of occurences using keywords = " .$count . '<br/>';
$count = 0;
}
更简单的数组_intersect:
$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.','Cristiano Ronaldo is World Soccer\'s Player of the Year 2013.','Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona');
foreach($titles as $title) {
if (count(array_intersect(explode(' ',strtolower($title)), $keywords)) >= 3) {
//stuff
}
}
使用数组_intersect更简单:
$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.','Cristiano Ronaldo is World Soccer\'s Player of the Year 2013.','Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona');
foreach($titles as $title) {
if (count(array_intersect(explode(' ',strtolower($title)), $keywords)) >= 3) {
//stuff
}
}
循环标题,在空格上分解,在分解的vs$KEYONS count()结果上运行array_intersect(),在空格上分解,在分解的vs$KEYONS count()结果上运行array_intersect(),但是为什么在第一个foreach末尾又写了$count呢loop@HasanZohdy不客气,这只会重置计数器。每个标题都有一个计数。所以在一个标题迭代之后,你需要重新设置它,为下一个标题做准备。在第二个foreach对我来说更有意义之前,只需设置一次就可以了。谢谢,伙计,但是为什么你在第一个foreach末尾又写了$count呢loop@HasanZohdy不客气,这只会重置计数器。每个标题都有一个计数。所以在一个标题的迭代之后,你需要重新设置它,为下一个标题做准备,只需设置一次,在第二个foreach对我来说更有意义之前。