I am trying to sort an array with array_multisort() but it only seems to sort the first column. I would like it to sort by 4 columns. Here is my function.
function array_sort_by_column(&$arr, $col, $col2, $col3, $col4, $dir = SORT_DESC) {
foreach ($arr as $key => $row) {
$sort_col[$key] = $row[$col];
$sort_col2[$key] = $row[$col2];
$sort_col3[$key] = $row[$col3];
$sort_col4[$key] = $row[$col4];
}
array_multisort($sort_col, $dir, $sort_col2, $dir, $sort_col3, $dir, $sort_col4, $dir, $arr);
}
The input here is a two dimensional array as $arr and then the column names I would like to sort by as $col1, $col2, $col3, and $col4.
EDIT: Sorry I meant 2 dimensional array
Sample:
$arr = array(
0 => array(
'group_id' => '1',
'points' => '5',
'rank' => '10',
'diff' => '1'
),
1 => array(
'group_id' => '1',
'points' => '1',
'rank' => '2',
'diff' => '4'
),
2 => array(
'group_id' => '2',
'points' => '1',
'rank' => '1',
'diff' => '2'
),
3 => array(
'group_id' => '2',
'points' => '9',
'rank' => '0',
'diff' => '-11'
),
);
array_sort_by_column($arr, 'group_id', 'points', 'rank', 'diff');