php really have no good multisort function
There is [man]array_multisort[/man]
but it will not take an array and sort it by 'name' for example
$data = array(
0 => array('name'=>'John', 'age'=>30,'city'=>'New York'),
1 => array('name'=>'Maria','age'=>27,'city'=>'Paris'),
2 => array('name'=>'Roger','age'=>33,'city'=>'London'),
3 => array('name'=>'Betty','age'=>21,'city'=>'London'));
Instead you need to make 3 arrays first:
$name = array('John','Maria','Roger','Betty');
$age = array(30,27,33,21);
$city = array('New York','Paris','London','London');
Then do the array_multisort() and the result is in $data
<?php
array_multisort($name,SORT_DESC, $age, $data);
echo '<pre>';
var_export($data);
?>
Now, this will not solve such multidim arrays as you have.
But if you want a good function for sorting 2-dimensional data rows
such rows with same structire as in Databasse results
then you should really consider some function
in the posted comment of http://php.net/manual/en/function.array-multisort.php
There are a number of versions of this multisort of array rows.
This is the version I use. It uses [man]eval[/man]
<?php
// A more inuitive way of sorting multidimensional arrays
// using array_msort() in just one line,
// you don't have to divide the original array into per-column-arrays
// Case insensitive by the use of strtolower()
$arr1 = array(
array('id'=>1,'name'=>'aA','cat'=>'cc'),
array('id'=>2,'name'=>'aa','cat'=>'dd'),
array('id'=>3,'name'=>'bb','cat'=>'cc'),
array('id'=>4,'name'=>'bb','cat'=>'dd')
);
$arr2 = array_msort($arr1, array('name'=>SORT_DESC, 'cat'=>SORT_ASC));
//function
function array_msort($array, $cols)
{
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
}
$eval = 'array_multisort(';
foreach ($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
?>