Skip to content

Instantly share code, notes, and snippets.

@fieg
Created October 11, 2011 22:09
Show Gist options
  • Save fieg/1279611 to your computer and use it in GitHub Desktop.
Save fieg/1279611 to your computer and use it in GitHub Desktop.
Sort a multi dimensional array on a column
<?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