Created
February 10, 2019 11:38
-
-
Save Septdir/dcb0eb393da227fc47b9b3e319235f01 to your computer and use it in GitHub Desktop.
sort array
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
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