Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 字符串比较和数组的更改顺序_Php_Regex_Arrays - Fatal编程技术网

Php 字符串比较和数组的更改顺序

Php 字符串比较和数组的更改顺序,php,regex,arrays,Php,Regex,Arrays,正在尝试为具有如下数组的用户创建搜索: 原始数组: array(2) { [1]=> array(1) { ["string"]=>"One two three, Blue, Green, Yellow" } [2]=> array(1) { ["string"]=>"One two four, Blue, Green, Yellow" } } 现在,我如何使用输入字段中的单词(可能

正在尝试为具有如下数组的用户创建搜索:

原始数组:

array(2) {
  [1]=>
      array(1) {
        ["string"]=>"One two three, Blue, Green, Yellow"
      }
  [2]=>
      array(1) {
        ["string"]=>"One two four, Blue, Green, Yellow"
      }
}
现在,我如何使用输入字段中的单词(可能是“一两个蓝色四个”)来执行正则表达式,然后更改给定的数组顺序(在本例中为):

原因原始数组[2]有更多匹配项。另外,如果用户写“一两个蓝色f”,我希望订单已经更改

我试过使用array_diff,但我需要一些相反的array_diff函数(显示所有匹配项),可能还需要一个正则表达式来处理单个字母


任何建议都将不胜感激

我将分解搜索词,并使用或对照数组中的字符串检查每个单词。出现次数较多的一个上升,如果出现次数较少,则下降。你可以用这个

您的分拣机可能看起来像这样:

class OccurrenceSorter {
    private $searchTerm;

    public function sort(array $arr, $searchTermStr) {
        $this->searchTerm = preg_split('/\s+/', $searchTermStr);

        usort($arr, array($this, 'doSort'));

        return $arr;
    }

    private function doSort($a, $b) {
        $aScore = $this->getScore($a['string']);
        $bScore = $this->getScore($b['string']);

        if($aScore == $bScore)
            return 0;

        return ($aScore < $bScore)?1:-1;
    }

    private function getScore($str) {
        $score = 0;
        $strLower = strtolower($str);

        foreach($this->searchTerm as $st) {
            // substr_count() or strpos() depending on the wished behavior 
            $score += substr_count($strLower, strtolower($st));
        }

        return $score;
    }
}

$arr = array(
    array('string' => 'One two three, Blue, Green, Yellow'),
    array('string' => 'One two four, Blue, Green, Yellow')
);

$searchTerm = 'one two blue four';

$rs = new OccurrenceSorter();
$sortedArr = $rs->sort($arr, $searchTerm);

var_dump($sortedArr);
比以下字符串“高”(尽管此字符串包含更多不同的搜索词):

第一个字符串总共有6个匹配项(二,四,四,四,蓝色,蓝色),第二个字符串有4个匹配项(一,二,四,蓝色)。如果这不是所希望的行为,请使用
strpos()
。然后你可以使用
substr\u count()
如果
a
b
strpos()和
b
是相同的,那么就可以得到一个比另一个更高的排名。

这简直太棒了(我走错了),非常感谢你,试图理解这段代码中发生了什么,也许会有一些问题
class OccurrenceSorter {
    private $searchTerm;

    public function sort(array $arr, $searchTermStr) {
        $this->searchTerm = preg_split('/\s+/', $searchTermStr);

        usort($arr, array($this, 'doSort'));

        return $arr;
    }

    private function doSort($a, $b) {
        $aScore = $this->getScore($a['string']);
        $bScore = $this->getScore($b['string']);

        if($aScore == $bScore)
            return 0;

        return ($aScore < $bScore)?1:-1;
    }

    private function getScore($str) {
        $score = 0;
        $strLower = strtolower($str);

        foreach($this->searchTerm as $st) {
            // substr_count() or strpos() depending on the wished behavior 
            $score += substr_count($strLower, strtolower($st));
        }

        return $score;
    }
}

$arr = array(
    array('string' => 'One two three, Blue, Green, Yellow'),
    array('string' => 'One two four, Blue, Green, Yellow')
);

$searchTerm = 'one two blue four';

$rs = new OccurrenceSorter();
$sortedArr = $rs->sort($arr, $searchTerm);

var_dump($sortedArr);
two four, four, four, Blue, Blue, Green, Yellow
One two four, Blue, Green, Yellow