Skip to content

Instantly share code, notes, and snippets.

@benrogmans
Last active August 29, 2015 13:58
Show Gist options
  • Save benrogmans/9975650 to your computer and use it in GitHub Desktop.
Save benrogmans/9975650 to your computer and use it in GitHub Desktop.
Sort multidimensional array by value php function
function sort_array($array, $columns) {
usort($array, function($a, $b) use ($columns) {
$t = array(true => -1, false => 1);
$r = true;
$k = 1;
foreach ($columns as $order => $value) {
$k = (strtolower($order) === 'desc') ? -1 : 1;
$r = ($a->{$value} < $b->{$value});
if ($a->{$value} !== $b->{$value}) {
return $t[$r] * $k;
}
}
return $t[$r] * $k;
});
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment