Sorting a php array from an sql query

i have this code creating my array…

$sql = "SELECT invoice_id, vehicle_id, type, date FROM invoice_haul WHERE date > ".$time;
if ( !($result = $db->sql_query($sql)) )
{
	die('Error in obtaining haul data');
}
while($haul = $db->sql_fetchrow($result))
{
	$row[] = $haul;
}

I cannot for the life of me figure out how to sort $row by the key $row[‘date’]… i know i can just ORDER BY the sql query but there are other queries that add to $row and i need to sort by the date after the fact.

You would want to use usort() for this. See the manual for example#2. We don’t know your date format so can’t help much more at this point.

ill take a look at that, the date format i use is the standard time(); / UNIX_TIMESTAMP format.

Ok, that makes it easy. When comparing numbers you can simply subtract them.


function cmp_timestamp($a, $b) {
    return $b['date'] - $a['date'];
}
usort($rows, 'cmp_timestamp');

You can also use create_function() if you want to avoid creating a global function for such a trivial operation, or use an anonymous function if you use php 5.3+