Created
January 3, 2020 16:26
-
-
Save fdorantesm/31a177cf0647b5a6fe8870eebc9ae391 to your computer and use it in GitHub Desktop.
Order by attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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