I have a large multidimensional array and I want to sort it twice by date using array_multisort
and get the last 3 arrays from each sort
I could create a duplicate of the array but it seems a waste when all I want is 3 arrays from it
$rows = array(
array(...),
array(...),
...
);
I create the arrays to be sorted like this
foreach($rows as $key => $row) {
$submit_date[$key] = $row['Submit_Date'];
$view_date[$key] = $row['View_Date'];
}
On this iteration of the sort, everything works as I expect
array_multisort($view_date, SORT_DESC, $rows);
$viewed = array_slice(array_unique($rows, SORT_REGULAR), 0, 3, true);
but on this one which is run straight after, I get different results to what I expect
array_multisort($submit_date, SORT_DESC, $rows);
$unlisted = array_slice(array_unique($rows, SORT_REGULAR), 0, 3, true);
I can't sort on both sort arrays because there will be occasions where $view_date array will have null values.
Is there a way I can use the same array to sort by view date, get the last 3 rows then sort the array by submit date then get the last 3 rows?
It's because your first multisort messed up the order of $rows
.
A dummy array should do the trick: $temp = $rows;