i have some problem to understand array_multisort
See how it sorts when two values are the same:
$a1=array(\"Dog\",\"Dog\",\"Cat\");
$a2=array(\"P
The entries in the second array corresponding to the identical entries in the first array.
If you look at the documentation and the first example, you'll notice that this is the expected behavior.
With two arguments, both arrays: the first array is sorted; the second array will have its corresponding values re-arranged and sorted if the corresponding values in first column tie. As for your example, think of it as you're doing a SQL ORDER BY Animal, Name
:
Well, you're sorting the arrays in a similar way to programs like Excel. Each array corresponds to a column.
First, all arrays are sorted by the first array given. If there are identical values, those affected are sorted by the second array given. If there are again equal values, the third array is used, etc.
Or in other words: The arrays are sorted using all arrays, but beginning on the right (if you assume it really sorts by all columns once).
For your particular example (the second one):
At first you want to sort in ascending order, so Cat
will be first. Therefore the last array element will be moved to the first position in both arrays. The other two elements, Dog
are equal. This causes the function to look at the next array. It's told to sort this array in descending order, so Pluto
comes first. In this case this leads to the result that the elements aren't moved at all (as their order is correct already).