Sorting multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thepiglet
Forum Newbie
Posts: 2
Joined: Thu May 20, 2004 4:57 am

Sorting multidimensional array

Post by thepiglet »

Hi,
Big problems here. I can't for the life of me get this thing to sort numerically. Here's a sample array:


$order[sandwich][45678][3][0][text] = 'Butter';
$order[sandwich][45678][1][0][text] = 'Cheddar Cheese';
$order[sandwich][45678][0][0][text] = 'Bread';
$order[sandwich][45678][1][1][text] = 'Leicester Cheese';
$order[sandwich][45678][9][0][text] = 'Pickle';

As you will see, the array takes the following steps:

sandwich- this means it is processed by the sandwichfunctions in the basket.
45678 - this is the unique ID for this sandwich.
1,3,0,9 - these are the category IDs of the components such as Cheeses stored in a mysql table.
0,0,0,1,0 - these are incremental so you can have more than one type of cheese for example.
text - this is the descriptive name for the component, others include ID which links to the item in a db.

OK, what I'm trying to do is sort this array by the 1,3,0,9 values (so it goes bread, cheese, butter, pickle) but am having no luck!
All the data is being messed up and I'm getting nowhere. Remember the can be other food types such as 'rolll', further 'roll' IDs etc..

Any ideas?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

use [php_man]array_multisort[/php_man], have a look at the user-notes there.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Yeah, it cannot be done with a single PHP command - I would recommend the Ichier function on the page patrik posted above, very handy indeed.
thepiglet
Forum Newbie
Posts: 2
Joined: Thu May 20, 2004 4:57 am

Post by thepiglet »

OK,
Thanks for the advice. I was bumbling around with array_multisort but didn't get as far as the notes :o)
I'll let you know how I get on.
Post Reply