<?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);
preferences:
24.25 ms | 402 KiB | 5 Q