I have my code working perfectly how I want it to, but the problem is my code is sorting by highest value to lowest. Can you help me reverse it so that when I print out the first 10 it is actually the "10 newest" (meaning the lowest duration)?
Thanks so much
function compareStreamDurations($a, $b)
{
if ($a["duration"] == $b["duration"])
{
return 0;
}
return ($a["duration"] > $b["duration"]) ? -1 : 1;
}
usort($onlineStreams, 'compareStreamDurations');
for ( $i=0; $i<10; $i++ )
{
echo '<p>', $onlineStreams[$i]["duration"] ,'</p>';
}
The solutions posted below (reversing the sign) are NOT working. I'm doing a print_r of $onlineStreams before and after the usort function call and they are both the same.
$onlineStreamsarray look like? Does the "duration" key exist in all array elements? Are the "duration" values numeric?>or<? Have you tried checking the return value of usort to make sure that the sort succeeded?