I have an array. It' looks like:
$arr = [
'0' => ['id'=>9, 'q'=>'motor', 'pos'=>1],
'1' => ['id'=>10, 'q'=>'NULL', 'pos'=>0],
'2' => ['id'=>7, 'q'=>'motor', 'pos'=>2],
'3' => ['id'=>8, 'q'=>'NULL', 'pos'=>0],
'4' => ['id'=>11, 'q'=>'motor','pos'=>3],
'5' => ['id'=>11, 'q'=>'exhaust','pos'=>1]
];
How can I sort the data above to make it looks like (if q='motor' is inside search string):
$arr = [
'0' => ['id'=>9, 'q'=>'motor', 'pos'=>1],
'2' => ['id'=>7, 'q'=>'motor', 'pos'=>2],
'4' => ['id'=>11, 'q'=>'motor','pos'=>3],
'1' => ['id'=>10, 'q'=>'NULL', 'pos'=>0],
'3' => ['id'=>8, 'q'=>'NULL', 'pos'=>0],
'5' => ['id'=>11, 'q'=>'exhaust','pos'=>1]
];
So:
function custom_sort($input_query, $arr) {
foreach($arr as $key=>$row) {
if (strpos($input_query, $row['q'])) {
... How to Sort By Pos?
}
}
}
Thanks!
Updated (question/answer) p.s: I'm trying to use custom variable as input: $q
usort($arr, function($a, $b) use ($q) {
if ($a['q'] == $q) {
if($b['q'] == $q) {
return ($a['pos'] > $b['pos']) ? 1 : -1;
}
return -1;
}
if ($b['q'] == $q) {
return 1;
}
return 0;
});
And the function stop working. How can I solve this?
Here's a usort
that handles your requirements, wrapped in a function that transforms the array in place:
Items with q
key containing $query
will be placed at the front of the array.
Items with q
key containing $query
will be sorted ascending by their pos
value.
Everything else will be sorted by its pos
value.
function custom_sort($query, &$arr) {
usort($arr, function($a, $b) use ($query) {
$in_a = strpos($a['q'], $query) !== false;
$in_b = strpos($b['q'], $query) !== false;
if ($in_a && !$in_b) {
return -1;
}
if (!$in_a && $in_b) {
return 1;
}
return $a['pos'] - $b['pos'];
});
}
custom_sort("motor", $arr);