Skip to content

Instantly share code, notes, and snippets.

@michaelaguiar
Created September 10, 2013 00:09
Show Gist options
  • Save michaelaguiar/6503270 to your computer and use it in GitHub Desktop.
Save michaelaguiar/6503270 to your computer and use it in GitHub Desktop.
Sort an array / object by key
<?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