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.
You can use
usort
— Sort an array by values using a user-defined comparison function oruksort
— Sort an array by keys using a user-defined comparison function oruasort
— Sort an array with a user-defined comparison function and maintain index associationEach of these accepts an array and a user-defined comparison function aka callback. What you put into the comparion function is up to you. As of PHP5.3 you can also use SplHeaps to create ordered collections.