This question is a bit old, but will leave the answer here for future.
From php.net-Multisort
function we can use the code below;
$data= [['volume' => 67, 'edition' => 2],['volume' => 85, 'edition' => 6],...];
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
The above is for a static sorting of data where you manually change sort columns.
For more dynamic and robust example consider below;
Assume I have the data below;
$data = [[1, 'Amanda', 'Wright', 'awright0@usnews.com', 'Female', '135.114.57.89', 31237],
[2, 'Theresa', 'Larson', 'tlarson1@51.la', 'Female', '207.108.96.210', 91011],
[3, 'Walter', 'Kennedy', 'wkennedy2@baidu.com', 'Male', '199.147.223.56', 50114],
[4, 'Andrea', 'Richards', 'arichards3@google.nl', 'Female', '230.195.124.95', 76489],
[5, 'Carol', 'Jones', 'cjones4@elegantthemes.com', 'Female', '250.197.111.90', 56501],
[6, 'Alice', 'Freeman', 'afreeman5@elegantthemes.com', 'Female', '52.195.252.131', 77170],
[7, 'Gerald', 'Fisher', 'gfisher6@slashdot.org', 'Male', '81.2.22.62', 75625],....]
If we need to sort the above array data out of the box, then we can set the sort orders in an array using the syntax;
$qTable[$index]=$sort_order;
E.g. $qTable=[1=>'asc',4=>'desc',3=>'asc'];
This means sort column 1 ASC, column 4 DESC then column 3 ASC.
We can then using the function below to sort our multidimension database data;
function sortMulti($data, $orders)
{
$args = [];
foreach ($data as $key => $row) {
foreach ($orders as $index => $order) {
if (!isset($row[$index])) continue; //Ignore if column does'nt exist
$args[$index]['d'][$key] = $row[$index]; //Get all values within the column
$args[$index]['o'] = 'desc' == strtolower($order) ? SORT_DESC : SORT_ASC; //Get the Sort order 'ASC' is the default
}
}
$p = [];
//Below we need to organize our entries as arguments for array_multisort
foreach ($args as $arg) {
$p[] = $arg['d'];
$p[] = $arg['o'];
//Below we need to check if column contains only numeric or not.
//If all values are numeric, then we use numeric sort flag, otherwise NATURAL
//Manipulate for more conditions supported
$p[] = count($arg['d']) == count(array_filter($arg['d'], 'is_numeric')) ? SORT_NUMERIC : SORT_NATURAL;
}
$p[] = &$data; //Pass by reference
call_user_func_array('array_multisort', $p); //Call Php's own multisort with parameters in required order.
return $data; //Our final array sorted.
}
Then we can use it as below;
$data=[[...],[...],...];
$order=[1=>'asc',4=>'desc',3=>'asc'];
$sorted=sortMulti($data,$order);
For key value array data E.g. $data=[['c1'=>1212,'c2'=>'mynames'],...];
Use the order as $order=['c1'=>'desc','c10'=>'asc'];
I tested the above with an array of 1000 records.
Hope it helps someone.