【问题标题】:PHP: Sort Multidimensional Array Based on Term in relation to a value of a keyPHP:根据与键值相关的术语对多维数组进行排序
【发布时间】:2019-04-14 02:16:51
【问题描述】:

我有以下数组,我想根据 $term = "geo" 对它进行排序,仅与 [result_title] 相关?

Array
(
    [0] => Array
        (
            [result_title] => Agathoklis Georgiou
            [result_subtext] => Active Employee
        )

    [1] => Array
        (
            [result_title] => Frixos Georgiou
            [result_subtext] => Active Employee
        )

    [2] => Array
        (
            [result_title] => George Ellinas
            [result_subtext] => Active Employee
        )

    [3] => Array
        (
            [result_title] => Georgi Georgiev
            [result_subtext] => Active Employee
        )

    [4] => Array
        (
            [result_title] => Charalambos Georgiou
            [result_subtext] => Former Employee
        )

    [5] => Array
        (
            [result_title] => Georgia Kantouna
            [result_subtext] => Former Employee
        )
)

期望的结果应该是:

Array
(
    [0] => Array
        (
            [result_title] => George Ellinas
            [result_subtext] => Active Employee

        )

    [1] => Array
        (
            [result_title] => Georgi Georgiev
            [result_subtext] => Active Employee


        )

    [2] => Array
        (
            [result_title] => Georgia Kantouna
            [result_subtext] => Former Employee
        )

    [3] => Array
        (
            [result_title] => Agathoklis Georgiou
            [result_subtext] => Active Employee
        )

    [4] => Array
        (
            [result_title] => Charalambos Georgiou
            [result_subtext] => Former Employee
        )

    [5] => Array
        (
            [result_title] => Frixos Georgiou
            [result_subtext] => Active Employee
        )
)

我尝试了各种方法,例如:

usort($data, function($a, $b) use ($term) {
    $x = strpos($a["result_title"], $term) === false;
    $y = strpos($b["result_title"], $term) === false;
    if ($x && !$y) return 1;
    if ($y && !$x) return -1;

    // use this if you want to sort alphabetically after the keyword sort:
    return strcmp($a["result_title"], $b["result_title"]);

    // or if you only want to sort by whether or not the keyword was found:
    return 0;
});

usort($data, function ($a, $b) use ($term) {
    similar_text($term, $a["result_title"], $percentA);
    similar_text($term, $b["result_title"], $percentB);

    return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
});

usort($data, function ($a, $b) use ($term) {
    $levA = levenshtein($term, $a["result_title"]);
    $levB = levenshtein($term, $b["result_title"]);

    return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1);
});

usort($data, function($a, $b){ return $a["result_title"] - $b["result_title"]; }); 

还有更多没有任何适当的结果。或者我可能无法理解达到我的结果的方法?

我还检查了:php sorting an array based on a stringHow to sort an array by similarity in relation to an inputted word.,但答案给了我正在寻找的结果。

【问题讨论】:

  • 你能提供你的源数组作为 PHP 代码吗?
  • @MarcinOrlowski 你的意思是数组最初是如何形成的?
  • 它来自哪里并不重要。将其放入有效的 PHP 数组中,这样我就可以尝试不浪费时间自己将这些数据转换为代码 :)
  • 数组 ( [0] => 数组 ( [result_title] => Agathoklis Georgiou [result_subtext] => 在职员工 ) [1] => 数组 ( [result_title] => Frixos Georgiou [result_subtext] = > 活跃员工 ) [2] => 数组 ( [result_title] => George Ellinas [result_subtext] => 活跃员工 ) [3] => 数组 ( [result_title] => Georgi Georgiev [result_subtext] => 活跃员工 ) [4 ] => 数组([result_title] => Charalambos Georgiou [result_subtext] => 前雇员))
  • @MarcinOrlowski 我希望这就是你的意思:S

标签: php arrays sorting multidimensional-array


【解决方案1】:

这是一个我认为可以满足您需求的功能。我假设您想在单词的开头找到$term 的值。此代码提取标题中包含$term 的任何关键字,然后根据是否找到关键字进行排序,然后按照关键字的顺序,或者如果它们都相同,则在标题上进行排序。

$term = 'geo';
usort($data, function ($a, $b) use ($term) {
    // find the term in first entry
    $t1 = preg_match("/^.*?\b($term\w*)\b.*\$/i", $a['result_title'], $matches) ? $matches[1] : '';
    // find the term in second entry
    $t2 = preg_match("/^.*?\b($term\w*)\b.*\$/i", $b['result_title'], $matches) ? $matches[1] : '';
    // check if the terms were found
    if ($t1 == '' && $t2 != '') return 1;
    if ($t1 != '' && $t2 == '') return -1;
    // found in both - if not the same, just sort on the keyword
    if ($t1 != $t2) return strcmp($t1, $t2);
    // found the same keyword, sort on the whole title
    return strcmp($a['result_title'], $b['result_title']);
});

由于输出很长(这是您要求的),所以我省略了它,但我做了一个demo on 3v4l.org

【讨论】:

  • 我不知道该如何感谢你。在过去的 48 小时里,我一直在努力。谢谢!
  • 如果我将术语字符串设置为较低会有什么不同吗?例如有一些混淆
  • @jQuerybeast 我不确定您所说的“将术语字符串设置为较低”是什么意思?
  • @jQuerybeast 我已经对答案进行了编辑,以允许该术语是整个单词。
  • @jQuerybeast 代码的工作方式它不会以这种方式排序,因为'LIA' < 'Lia'。如果您希望它不区分大小写,请将 strcmp 更改为 strcasecmp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多