473,788 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a Multidimensiona l 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 13660
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
multidimensiona l 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 multidimensiona l 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
2010
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
2651
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 klicking a button or checking a radio button, whether the table shall be sorted by column 1 och 2. Even if the table has three or more columns I'm only interested in sorting (increasingly) by first or second. I would be nice if I did not have to...
1
2146
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 names by rows and subjects/course units by columns.How should I organise my c++ code.The program should sort the grades of each students in ascending or descending orders.
6
3542
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 3 Row3-> 3 9 8 7 Row4-> 8 2 4 8
9
2615
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. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
16
3625
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
1757
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 from 2 different tables.i am getting the output properly but only thing is tht i need to sort the o/p as per name here fid=6 is designation..uid is the id of the user ,fid=3 is Name of the person,fid=11 is for extension ,fid=1 is research...
7
7024
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. Thanks, Pradeep
9
4503
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 function: x = new int;
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.