I have 4 arrays and use array_multisort to sort them all at the same time relative to one another.
Problem is, in the first array, there can be empty values and I want to put them at the end, not the beginning.
Example : http://codepad.org/V6TjCsS5
Is there a way to:
- Pass a custom function to array_multisort
or - Sort the first array with a custom function then use the result order to sort the other arrays
or - Use a certain argument with array_multisort to achieve what I want
Thank you very much
Unfortunately none of the approaches your propose is possible, which means you have to take another step back and look for alternatives. I am assuming you want a normal ascending sort, with the explicit exception that empty elements (which you be “smallest”) need to be considered as “largest”.
Option 1: Manually rearrange elements after sorting
Do your
array_multisortas usual, and then make the modifications you require:You can pull out part of the code in a function to make this prettier:
Option 2: Condense everything inside one array so you can use
usortThe idea here is to pull all your arrays into one so you can specify the comparison function by using
usort:You can now sort:
At this point you are left with an array, each element (row) of which is an array. The first elements of each are what was originally inside
$array1, second elements are what was in$array2, etc. By placing those elements inside “rows”, we have managed to keep the sort order among all your original arrays synchronized.