[PHP] Illustration array_multisort()
preface
array_multisort() is one of the many array sorting functions, but
the manual to find out how to use it , I couldn't really understand it, so I explained it with an illustration.
So, this is an explanation for those who don't understand array_multisort() well.
simple example
$arr1 = [10, 100, 1, 0]; $arr2 = [3, 2, 1, 0];
Let's sort these two arrays using array_multisort().
array_multisort($arr1, $arr2);
First, $arr1 is sorted.Since
the sorting method and sorting order are not specified with arguments, the comparison will be gradual and in ascending order.
The following image illustrates the movement of elements before and after sorting $arr1.
At this time,
(B) 1st element is 3rd after sorting
(C) 2nd element is 1st after sorting
(D) 3rd element is 0th after sorting
A rule is created and applied when sorting $arr2.
The movement of elements before and after sorting $arr2 is as follows.
If one more array is passed as an argument, the sorting result of $arr2 will affect the sorting of that array.
difficult example
$arr3 = [500, 100, 250, 0, 500, 100]; $arr4 = [5, 4, 3, 2, 1];
Let's sort these two arrays using array_multisort().
array_multisort($arr3, $arr4);
First, $arr3 is sorted.As
in the previous example, the sorting method and sorting order are not specified with arguments, so the comparison is gradual and in ascending order.
The following image illustrates the movement of elements before and after sorting $arr3.
At this time,
(b) The 1st and 5th elements are the 1st or 2nd after sorting
(c) The 2nd element is the 3rd after sorting
(d ) 3rd element is 0th after sorting
A rule is created and applied when sorting $arr4.
The big difference from the previous example is that $arr3 has two sets of the same numbers.
Since 100 and 100, and 500 and 500 are the same size (= comparison results are equal),
at this point the placement destination after sorting is undefined as in rules (a) and (b).
Now, what will be the sort result of $arr4?
Rules (c) and (d) will continue to be enforced.
Regarding (a), the sorting method specified by the argument is applied to the corresponding element (4,0).
In this example, the sorting method for $arr4 is not specified, so the default is ascending order.
Similarly, in (b), the corresponding elements (5,1) are arranged in ascending order.
As a result of sorting $arr4, subsequent arrays (if passed as arguments) are
(a-2) The 4th element is the 4th after sorting
(b-1) The 1st and element are the 2nd after sorting
(b-2) The 5th and Element is 1st after sorting
(c) 2nd element is 3rd after sorting
(d) 3rd element is 0th after sorting
That rule applies.
Amazing
supplement
$arr5 = [0, false]; $arr6 = [100, 200];
There is an array called
array_multisort($arr5, $arr6);
As a result,
Even if the contents of $arr5 become [false, 0],
the contents of $arr6 will not become [200, 100].
Because in a loose comparison 0 and false are equal.
Please refer to the manual regarding this operation
The end