Skip to content

Instantly share code, notes, and snippets.

@fdorantesm
Created January 3, 2020 16:26
Show Gist options
  • Save fdorantesm/31a177cf0647b5a6fe8870eebc9ae391 to your computer and use it in GitHub Desktop.
Save fdorantesm/31a177cf0647b5a6fe8870eebc9ae391 to your computer and use it in GitHub Desktop.
Order by attribute
<?
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment