是否有任何PHP函数可以解决另一个数组值字符串中的数组值匹配问题? [英] Is there any PHP function to solve out array value matches in another array value string?

查看:138
本文介绍了是否有任何PHP函数可以解决另一个数组值字符串中的数组值匹配问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建单词搜索,但我想根据搜索关键字的最高存在对它们进行排名.我怎么解决这个问题?

I am creating a word search but I want to rank them base on the highest existence of search keyword. How can I solve this problem?

我正在尝试搜索数组2长字符串中是否存在数组1键,然后按数组2在数组1中的总出现次数对数组进行排序.

I am trying to make a search if array 1 key exists inside array 2 long string and then order the array by total occurrence of array 1 in array 2.

打击是我的代码

$str = "Hello World January Jude";
$arr1 = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$str = explode(" ", $str);
echo sort_base($arr1, $str);
function sort_base($arr, $str){
$count = "";
foreach ($arr as $valuer){
    foreach ($str as $value){
    //$list[] = strpos($valuer, $value, 0);
    $count .= strpos($valuer, $value, 0)."<hr/>"; 
}

}
$arr = trim($count," ");
 echo $arr;
}

示例输入:

$array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; 
$str = "Hello World Cup"; 

期望输出:

按顺序排列:

  1. 你好世界杯
  2. 最终的Hello World
  3. 你好杯
  4. 你好
  5. 世界
  6. 问好

推荐答案

您可以使用 count 实现类似单词的数字值.现在,您可以使用 usort 进行排序.

You can use array-intersect and count to achieve number value of the similar words. Now you can use usort for sort by that.

请考虑以下内容:

function sort_most_exists_asc($arr, $str) {
    usort($arr, function ($a, $b) use ($str) {
        $aa = count(array_intersect(explode(" ", $str), explode(" ", $a)));
        $bb = count(array_intersect(explode(" ", $str), explode(" ", $b)));
        return $bb - $aa;
    });
    return $arr;
}

$str = "Hello World January Jude";
$arr = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$arr = sort_most_exists_asc($arr, $str);

实时示例: 3v4l

请注意,这仅适用于整个单词.对于相似的单词,请使用 Levenshtein距离-并在usort

Notice this will work only for whole words. For words similarity use Levenshtein distance - and compare by that in the usort

这篇关于是否有任何PHP函数可以解决另一个数组值字符串中的数组值匹配问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆