From PHP manual
<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>
In this example, we will order by volume descending, edition ascending.
We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
http://php.net/manual/en/function.array-multisort.php
Edit
For your need you requested:
$arr[4]=array('name' => 'ab', 'aid' => 1, 'qty' => 10);
$arr[5]=array('name' => 'ac', 'aid' => 3, 'qty' => 15);
$arr[3]=array('name' => 'ad', 'aid' => 2, 'qty' => 100);
$arr[2]=array('name' => 'ae', 'aid' => 2, 'qty' => 150);
$keyPositions = array();
foreach ($arr as $key => $value) {
$keyPositions[] = $key;
}
foreach ($arr as $key => $row) {
$aid[$key] = $row['aid'];
}
array_multisort($aid, SORT_ASC, $arr);
$sortedArray = array();
foreach ($arr as $key => $value) {
$sortedArray[$keyPositions[$key]] = $value;
}
echo '<pre>';
print_r($sortedArray);
echo '</pre>';
?>