phparrayssorting

Custom sort a flat array determined by user input


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.


Solution

  • You can use

    Each 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.