<?php
$data = array
(
array
(
'result_title' => 'Agathoklis Georgiou',
'result_subtext' => 'Active Employee'
),
array
(
'result_title' => 'Frixos Georgiou',
'result_subtext' => 'Active Employee'
),
array
(
'result_title' => 'George Ellinas',
'result_subtext' => 'Active Employee'
),
array
(
'result_title' => 'Fred Ellinas',
'result_subtext' => 'Active Employee'
),
array
(
'result_title' => 'Georgi Georgiev',
'result_subtext' => 'Active Employee'
),
array
(
'result_title' => 'Charalambos Georgiou',
'result_subtext' => 'Former Employee'
),
array
(
'result_title' => 'Georgia Kantouna',
'result_subtext' => 'Former Employee'
)
);
$term = 'geo';
usort($data, function ($a, $b) use ($term) {
// find the term in first entry
$t1 = preg_match("/^.*?\b($term\w+).*\$/i", $a['result_title'], $matches) ? $matches[1] : '';
// find the term in second entry
$t2 = preg_match("/^.*?\b($term\w+).*\$/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']);
});
print_r($data);
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- 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
)
[6] => Array
(
[result_title] => Fred Ellinas
[result_subtext] => Active Employee
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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
)
[6] => Array
(
[result_title] => Fred Ellinas
[result_subtext] => Active Employee
)
)
preferences:
254.32 ms | 404 KiB | 293 Q