Created
March 24, 2020 16:58
-
-
Save pranid/99ed79676952f81d1c28f6f530295bc0 to your computer and use it in GitHub Desktop.
PHP - Multidimensional array sorting by specific key
This file contains 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 | |
/** | |
* @param array $array | |
* @param string $_key | |
* @param string $_direction | |
* @version 1.0.0 | |
* @author Praneeth Nidarshan ([email protected]) | |
*/ | |
function multiArraySort(&$array, $_key,$_direction = 'desc') | |
{ | |
usort($array, function ($a, $b) use ($_key, $_direction) { | |
$a = $a["{$_key}"]; | |
$b = $b["{$_key}"]; | |
if ($a == $b) { | |
return 0; | |
} | |
if ($_direction == 'desc') { | |
return ($a < $b) ? 1 : -1; | |
} else { | |
return ($a < $b) ? -1 : 1; | |
} | |
}); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment