views:

56

answers:

3

Is there any way to "custom" sort an array? For instance, if I have the following numbers:

$array = array('0' => 1, '1' => 2, '2' => 3);

I would like it to order them in this fashion:

$array = array('0' => 2, '1' => 1, '2' => 3);

How would I do this or is it not possible? I am basically wanting to list this array in one database field for each user, but each order will be different depending on how the user sorts the array.

Thanks, Jake

+4  A: 
Gordon
Thank you! I will look into these :)
Jake
A: 

you may also want to have a look at array_multisort()

akellehe
A: 

It might be better to let the database sort the array upon retrieval with the users settings. I think that is almost always preferable over sorting in PHP.

edit: I forgot that the array is probably stored as a single object so the database just handles it as an object and not an array. To still enable serverside (database server) sorting, arrays could also be expressed as seperate tables:

userID : index0 userID : index1 etc...

but this might give too much data overhead.

Erik1984