PHP:根据与键值相关的术语对多维数组排序
我有以下数组,我想根据$term=“geo”对其进行排序,仅与PHP:根据与键值相关的术语对多维数组排序,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,我有以下数组,我想根据$term=“geo”对其进行排序,仅与[result\u title]相关 Array ( [0] => Array ( [result_title] => Agathoklis Georgiou [result_subtext] => Active Employee ) [1] => Array ( [resu
[result\u 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;
});
及
及
及
还有更多的没有任何适当的结果。或者也许我不明白实现我的结果的方法
我也检查过了:但是答案给出了我想要的结果。这里有一个函数,我认为它可以满足您的需要。我假定您希望在单词开头找到
$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']);
});
由于输出很长(这是您要求的),所以我省略了它,但我做了一个修改。您可以将源数组作为PHP代码提供吗?@MarcinOrlowski您的意思是数组最初是如何形成的?它来自何处并不重要。把它放在有效的PHP数组中,这样我就可以自己尝试不浪费时间将这些数据转换成代码:)数组([0]=>array([result\u title]=>Agathoklis Georgiou[result\u subtext]=>Active Employee)[1]=>array([result\u title]=>Frixos Georgiou[result\u subtext]=>Active Employee)[2]=>array([result\u title]=>George Ellinas[result\u subtext]=>在职员工[3]=>数组([result\u title]=>Georgi Georgiev[result\u subtext]=>Active Employee)[4]=>数组([result\u title]=>Charalambos Georgiou[result\u subtext]=>前任员工])@MarcinOrlowski我希望这就是你的意思:我不知道如何感谢你。在过去的48小时里我一直在努力。非常感谢。如果我将术语字符串设置为较低,会有不同吗?这其中有一些困惑example@jQuerybeast我不确定“将术语字符串设置为较低”是什么意思?@jQuerybeast我已对答案进行了编辑,以允许术语是整个单词。@jQuerybeast代码的工作方式它不会按这种方式排序,因为
'LIA'<'LIA'
。如果希望它不区分大小写,请将strcmp
更改为strcasecmp
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"]; });
$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']);
});