467,894 Members | 1,762 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

Sorting a Multidimensional Array

I have an array like this:

$events = array(
'Event Title 1',
'1' //ID Number (not unique)
'Event Title 3',
'Event Title 3',
I want to sort it by date. How would I do it?

I read the documentation for usort(), but I can't get it to work right.
Any suggestions?
Jul 16 '05 #1
  • viewed: 13255
1 Reply
Thank you so much, Jason. I didn't understand what the extra function
was for until you posted that.

It works perfectly, now. Thanks again.

Sorting by date from the usort docs on php.net - sorting a
multidimensional array:

function cmp ($a, $b) {
//return strcmp($a["fruit"], $b["fruit"]);
// in the case of the above array
// note we are comparing the first element of each array
if ( $a[0] < $b[0] ) {
return -1;
if ( $a[0] > $b[0] ) {
return 1;
// they are equal
return 0;

// we're using the array supplied above by Brian
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

// again, using data supplied by Brian
usort($events, "cmp");
//usort($fruits, "cmp");

This should sort the multidimensional array by the first element (index
0). Incidentally, you could make it sort in descending order by
switching the 1 and -1 in the "cmp" function.

Also, doing this in objects gets kind of tricky, read the user supplied
notes, there's a lot of useful stuff in there.


Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by MM | last post: by
1 post views Thread by Gaspard Kayitare | last post: by
6 posts views Thread by Index | last post: by
9 posts views Thread by Dylan Parry | last post: by
16 posts views Thread by Rehceb Rotkiv | last post: by
1 post views Thread by pradeepjain | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.