[PHP] Illustration array_multisort()

table of contents
preface
array_multisort() is one of the many array sorting functions, but
the manual to help me understand it.
So, this is an explanation for people who don't understand array_multisort().
A simple example
$arr1 = [10, 100, 1, 0]; $arr2 = [3, 2, 1, 0];
Let's try sorting these two arrays using array_multisort()
array_multisort($arr1, $arr2);
First, $arr1 is sorted.
Since the sort method and sort order are not specified as arguments, it is a loose comparison and ascending order.
The following image shows the movement of elements before and after sorting $arr1.

At this time,
(B) The 1st element is the third element after sorting.
(C) The 2nd element is the first element after sorting.
(D) The 3rd element is the 0th element after sorting.
The following rule will be created and applied when sorting $arr2.
The behavior of elements in $arr2 before and after sorting is as follows:

If you pass another array as an argument, the sorting of that array will be affected by the sorting results of $arr2
A difficult example
$arr3 = [500, 100, 250, 0, 500, 100]; $arr4 = [5, 4, 3, 2, 1];
Let's try sorting these two arrays using array_multisort()
array_multisort($arr3, $arr4);
First, $arr3 is sorted.
As with the previous example, the sort method and sort order are not specified as arguments, so the sort is performed in loose comparison and 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 either 1st or 2nd after sorting.
(c) The 2nd element is 3rd after sorting.
(d) The 3rd element is 0th after sorting.
This creates a rule that is applied when sorting $arr4
The big difference from the previous example is that $arr3 contains two sets of the same numerical values.
100 and 100, and 500 and 500 are the same size (= the comparison results are equal), so
at this point the placement after sorting is undefined according to rules (a) and (b).
So what will be the sorted result of $arr4? It will be like this

Rules (c) and (d) are executed as is.
For (a), the sorting method specified in the argument is applied to the corresponding element (4,0).
In this example, the sorting method for $arr4 is not specified, so it is sorted in ascending order by default.
Similarly, for (b), the corresponding element (5,1) is sorted in ascending order.
As a result of sorting $arr4, the following array (if passed as an argument)
(a-2) The 4th element is the 4th after sorting.
(b-1) The 1st and 2nd elements are the 2nd after sorting.
(b-2) The 5th and 1st elements are the 1st after sorting.
(c) The 2nd element is the 3rd after sorting.
(d) The 3rd element is the 0th after sorting.
The following rules apply:
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].
This is because 0 and false are equal in a loose comparison.
Please refer to the manual for details on this behavior
The end
0