Created
October 11, 2011 22:09
-
-
Save fieg/1279611 to your computer and use it in GitHub Desktop.
Sort a multi dimensional array on a column
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 | |
/* | |
* Sort a multidimensional array on a column | |
* | |
* For example: | |
* <code> | |
* <?php array_qsort2($users, "username", "ASC"); ?> | |
* </code> | |
* | |
* @param array $array array with hash array | |
* @param mixed $column key that you want to sort on | |
* @param enum $order asc or desc | |
*/ | |
function array_qsort2 (&$array, $column=0, $order="ASC") | |
{ | |
$oper = ($order == "ASC")?">":"<"; | |
if(!is_array($array)) return; | |
usort($array, create_function('$a,$b',"return (\$a['$column'] $oper \$b['$column']);")); | |
reset($array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment