473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting a Multidimensional Array

I have an array like this:

$events = array(
array(
'2003-07-01',
'Event Title 1',
'1' //ID Number (not unique)
),
array(
'2003-07-02',
'Event Title 3',
'22'
),
array(
'2003-07-06',
'Event Title 3',
'35'
)
);
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
1 13611
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:

<begin>
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");
<end>

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.

Jason


Jul 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: markus | last post by:
I usually store info in arrays using field names $array='Germany', $array="GE"; $array='United Sates" $array="US"; $array='France" $array='FR"
2
by: MM | last post by:
Hi there, Is it at all possible to sort the contents of a table (ordinary html <table><tr><td>...</td></tr></table>) using javascript? What I want is to be able to select, for example by...
1
by: Gaspard Kayitare | last post by:
I would like to know how to use multidimensional arrays principles for sorting grades for four students obtained in four subjects.I am supposed to arrange the data in a form of a table,students...
6
by: Index | last post by:
Hi, I have a 4 dimensional array and I want to sort it by the first two columns emulating an ORDER BY clause to some extent.Any suggestion? The original Array is: Row1-> 4 8 2 9 Row2-> 4 5 5...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
16
by: Rehceb Rotkiv | last post by:
Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb
1
pradeepjain
by: pradeepjain | last post by:
Hii, i need help in sorting multidimensianal array... i Read the multi sort function cld not use tht so i need help..This is the code. here the problem is tht i need to take out values...
7
pradeepjain
by: pradeepjain | last post by:
Hello Guys, I am using a multidimensional array in which there is a date field also..i want to sort the multidimensional array based on this date filed how to do this. ...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.