简体   繁体   English

PHP:基于术语对键值进行多维数组排序

[英]PHP: Sort Multidimensional Array Based on Term in relation to a value of a key

I Have the following Array and I would like to sort it based on the $term = "geo" in relation only to [result_title] ? 我有以下数组,我想基于$ 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
        )
)

The desired result should be: 理想的结果应该是:

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
        )
)

I have tried various methods such as: 我尝试了各种方法,例如:

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;
});

and

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);
});

and

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);
});

and

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

and many more without any proper result. 还有更多没有任何适当结果的结果。 Or maybe I cannot understand the method to achieve my result? 还是我不明白实现结果的方法?

I have also checked: php sorting an array based on a string and How to sort an array by similarity in relation to an inputted word. 我还检查了以下内容: php根据字符串 对数组进行排序,以及如何根据与输入单词的相似性对数组进行排序。 but the answers are giving me the result I'm looking for. 但是答案给了我想要的结果。

Here's a function that I think will do what you want. 我认为这是一个可以完成您想要的功能。 I've presumed you want to find the value of $term at the beginning of a word. 我假设您想在单词开头找到$term的值。 This code extracts any keyword in the title which includes $term and then sorts based on whether the keyword was found, followed by the ordering of the keyword, or if they are both the same, on the title. 此代码提取标题中包含$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']);
});

Since the output is long (it is what you asked for) I've omitted it but I've made a demo on 3v4l.org . 由于输出很长(这是您所要求的),因此我省略了它,但是在3v4l.org上进行了演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM