PDA

View Full Version : Sorting arrays


Darth Cow
08-02-2002, 12:36 AM
I have this massive multikeyed array. I've loaded up arrays with the stats for various sites into $sites[$siteid], where $siteid is just a number to keep track of things. In there, I have set $sites[$siteid]["lastupdatetime"] for each (the time they were last updated, in seconds from 1970). I want to sort the various arrays in $sites[$siteid] by their respective values for $sites[$siteid]["lastupdatetime"], so I can just loop through $site[$siteid] and it'll be ordered right by date.

I'm kinda a PHP newbie (at least in some respects :D), so I've tried looking at the PHP documentation for sort() and its varients, but I'm baffled by how to sort my massive array in the way I want :(.

Thanks in advance!

Darth Cow
08-03-2002, 03:22 AM
Never mind... I finally found a solution in the comments in the PHP documentation for one of the sort functions.

I needed to use "usort", like so:

function sort_it($a, $b) {
return strcmp($b["lastupdatesort"], $a["lastupdatesort"]);
}

usort($sites, "sort_it");

Heh.. I knew there was a solution somewhere... I just wasn't finding it :(.