Sorting multi-dimensional array by two values
Posted: Tue Oct 26, 2004 5:17 am
Hi,
I have a database with two tables. Each row in both tables has an associated time and date value entered in separate columns.
I run a SELECT query on the first table, then using php feed the values into a multi-dimensional array and check to see if a value exists in the each row of the result set from the first query. If it does I do a second SELECT query on the second table and overwrite the relevant entries from the first result set to leave me with one result set in a multi-dimensional array.
I need to reorganise the array by date and time so that the most recent result is the first result.
I've managed to do this by date OR time by using usort() and the following:
However I can't get it to sort by both. I've tried nesting an 'if ' statement to attempt to only check if $x['Time'] /$y['Time'] is different if $x['Day']/ $y['Day'] is the same but to not avail.
The only way round it I can think of is by doing some kind of initial query, running through the results with php then doing a UNION query on both tables and letting SQL order them but this doesn't seem very efficient to me as I'd be having to get the result set from the first table twice.
Any suggestions?
I have a database with two tables. Each row in both tables has an associated time and date value entered in separate columns.
I run a SELECT query on the first table, then using php feed the values into a multi-dimensional array and check to see if a value exists in the each row of the result set from the first query. If it does I do a second SELECT query on the second table and overwrite the relevant entries from the first result set to leave me with one result set in a multi-dimensional array.
I need to reorganise the array by date and time so that the most recent result is the first result.
I've managed to do this by date OR time by using usort() and the following:
Code: Select all
<?php
function sortdates ($x, $y)
{
if ( $x['Day'] == $y['Day'] )
return 0;
elseif ( $x['Day'] <$y['Day'] )
return 1;
else
return -1;
}
?>The only way round it I can think of is by doing some kind of initial query, running through the results with php then doing a UNION query on both tables and letting SQL order them but this doesn't seem very efficient to me as I'd be having to get the result set from the first table twice.
Any suggestions?