Sorts a collection of arrays or objects by key.
Uses sort()
on the provided array to sort the array based on $order
and $attr
.
代码实现
function orderBy($items, $attr, $order)
{
$sortedItems = [];
foreach ($items as $item) {
$key = is_object($item) ? $item->{$attr} : $item[$attr];
$sortedItems[$key] = $item;
}
if ($order === 'desc') {
krsort($sortedItems);
} else {
ksort($sortedItems);
}
return array_values($sortedItems);
}
使用样例
orderBy(
[
['id' => 2, 'name' => 'Joy'],
['id' => 3, 'name' => 'Khaja'],
['id' => 1, 'name' => 'Raja']
],
'id',
'desc'
); // [['id' => 3, 'name' => 'Khaja'], ['id' => 2, 'name' => 'Joy'], ['id' => 1, 'name' => 'Raja']]