Created
September 10, 2013 00:09
-
-
Save michaelaguiar/6503270 to your computer and use it in GitHub Desktop.
Sort an array / object by key
This file contains hidden or 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
<?php | |
public function sort_by_key($key, $array, $sort_by) { | |
for ($i = 0; $i <= count($array) - 1; $i++) { | |
if (is_object($array[$i])) { | |
$array_item = $array[$i]->{$key}; | |
} else { | |
$array_item = $array[$i][$key]; | |
} | |
$sort[$i] = (isset($array_item)) ? strtolower($array_item) : ''; | |
} | |
array_multisort($sort, $sort_by, $array); | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment