I found an array sorting function on the php manual site which does exactly what I want, but it generates warnings and I have been trying to work out why and how to stop them. Any advice much appreaciated.
function fnArrayOrderby(){
//function to sort a database type array of rows by the values in one or more column
//source http://php.net/manual/en/function.array-multisort.php - user notes
//example of use -> $sorted = fnArrayOrderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
$args = func_get_args(); //Gets an array of the function's argument list (which can vary in length)
//echo "sorting on ".$args[1];
$data = array_shift($args); //Shift an element off the beginning of array
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
the warnings generated are
- PHP Warning: Invalid argument supplied for foreach() in .. for the line " foreach ($data as $key => $row)"
- PHP Warning: array_multisort(): Argument #5 is expected to be an array or a sort flag ... for the line call_user_func_array('array_multisort', $args)