我需要一个按优先级排序项目数组的方法。
以下是迄今为止我所做的工作:
function arraySortPriority(array &$array, $offset, array $priorities)
{
uasort($array, function ($a, $b) use ($offset, $priorities) {
if (!isset($a[$offset])) {
$a[$offset] = null;
}
if (!isset($b[$offset])) {
$b[$offset] = null;
}
if ($a[$offset] == $b[$offset]) {
return 0;
}
$aPriority = isset($priorities[$a[$offset]])
? $priorities[$a[$offset]]
: null;
$bPriority = isset($priorities[$b[$offset]])
? $priorities[$b[$offset]]
: null;
return $aPriority > $bPriority ? -1 : 1;
});
}
// an array to sort
$array = [
['type' => 'A'],
['type' => 'A'],
['type' => 'B'],
['type' => 'B'],
['type' => 'C'],
['type' => 'C'],
['type' => 'D'],
['type' => 'D'],
['type' => 'E'],
['type' => 'E'],
['type' => 'F'],
['type' => 'F'],
['type' => 'G'],
['type' => 'G'],
['type' => 'H'],
['type' => 'H'],
['type' => 'Foo'],
['type' => 'Foo'],
['type' => 'Bar'],
['type' => 'Bar'],
[0 => 'no type should be last'],
[0 => 'no type should be last'],
];
// shuffle the array
shuffle($array);
// set priorities
$priorities = [
'A' => 8,
'B' => 7,
'C' => 6,
'D' => 5,
'E' => 4,
'F' => 3,
'G' => 2,
'H' => 1,
];
// call
arraySortPriority($array, 'type', $priorities);
// test output
foreach ($array as $item) {
if (isset($item['type'])) {
echo "{$item['type']}\r\n";
} else {
$values = array_values($item);
echo reset($values) . PHP_EOL;
}
}
预期:
A
A
B
B
C
C
D
D
E
E
F
F
G
G
H
H
Foo
Foo
Bar
Bar
no type should be last
no type should be last
实际:
A
A
B
B
C
C
D
D
E
E
F
F
G
G
H
H
no type should be last
no type should be last
Bar
Bar
Foo
Foo
问题是,未提供的项
$offset
应始终按底部排序。
这意味着
no type should be last
排序应始终低于
Foo
或
Bar
.
我该怎么做?