Skip to content

Instantly share code, notes, and snippets.

@Septdir
Created February 10, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Septdir/dcb0eb393da227fc47b9b3e319235f01 to your computer and use it in GitHub Desktop.
Save Septdir/dcb0eb393da227fc47b9b3e319235f01 to your computer and use it in GitHub Desktop.
sort array
public function arraySort($array = array(), $fields = array())
{
if (!empty($array) && !empty($fields))
{
usort($array, function ($a, $b) use ($fields) {
$res = 0;
foreach ($fields as $k => $v)
{
if ($a->$k == $b->$k) continue;
$res = ($a->$k < $b->$k) ? -1 : 1;
if ($v == 'desc') $res = -$res;
break;
}
return $res;
});
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment