1 /** 2 I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort(). 3 */ 4 5 <?php 6 function array_orderby() 7 { 8 $args = func_get_args(); 9 $data = array_shift($args); 10 foreach ($args as $n => $field) { 11 if (is_string($field)) { 12 $tmp = array(); 13 foreach ($data as $key => $row) 14 $tmp[$key] = $row[$field]; 15 $args[$n] = $tmp; 16 } 17 } 18 $args[] = &$data; 19 call_user_func_array('array_multisort', $args); 20 return array_pop($args); 21 } 22 ?> 23 24 The sorted array is now in the return value of the function instead of being passed by reference. 25 26 <?php 27 $data[] = array('volume' => 67, 'edition' => 2); 28 $data[] = array('volume' => 86, 'edition' => 1); 29 $data[] = array('volume' => 85, 'edition' => 6); 30 $data[] = array('volume' => 98, 'edition' => 2); 31 $data[] = array('volume' => 86, 'edition' => 6); 32 $data[] = array('volume' => 67, 'edition' => 7); 33 34 // Pass the array, followed by the column names and sort flags 35 $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC); 36 ?>