[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[PHP] Illustration array_multisort()

PHP

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,

(A) 0th element is 2nd after sorting
(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,

(a) The 0th and 4th elements are the 4th or 5th after sorting
(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-1) The 0th element is the 5th after sorting
(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

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
637
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author