|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// mdasort(&$array, $sort_by_key, $direction = SORT_ASC)
// ----------------------------------------------------------------------------------
// returns true/false
//
// &$array = array to be sorted
// $sort_by_key = the key of the array element to sort by. This element is an element
// of the second-dimension array
// $direction = SORT_ASC or SORT_DESC
function mdasort(&$array, $sort_by_key, $direction = SORT_ASC)
{
try
{
$comparisons = array();
foreach($array as $comp)
{
$comparisons[] = $comp[$sort_by_key];
}
array_multisort($comparisons, $direction, $array);
return true;
}
catch (Exception $e)
{
echo $e;
return false;
}
} |
Here is an example of its useage…
|
1 2 3 4 5 6 7 8 9 10 |
$arr = array(
array('lemon'),
array('apple'),
array('watermelon'),
array('grape')
);
mdasort($arr, 0, SORT_DESC);
print_r($arr); |
Will look like…
Array
(
[0] => Array
(
[0] => watermelon
)
[1] => Array
(
[0] => lemon
)
[2] => Array
(
[0] => grape
)
[3] => Array
(
[0] => apple
)
)