Created
June 25, 2021 19:12
-
-
Save angelorocha/f427db119aa635fa60f41cda593c742a to your computer and use it in GitHub Desktop.
Order multidimensional array by 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 | |
/** | |
* Order array by key | |
* @param $array | |
* @param $array_key | |
* @param string $order | |
* | |
* @return bool | |
*/ | |
function custom_array_multisort(&$array, $array_key, $order = 'desc'){ | |
return usort($array, function ($a, $b) use ($array_key, $order){ | |
return ('desc' === $order ? strnatcmp($b[$array_key], $a[$array_key]) : strnatcmp($a[$array_key], $b[$array_key])); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment