Backend Development 5 min read

How to Use PHP’s array_multisort() Function to Sort Multiple Arrays

This article explains the syntax and parameters of PHP’s array_multisort() function, demonstrates its usage with a practical example of sorting student names, ages, and scores, and shows the resulting output, highlighting how to sort multiple arrays simultaneously.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP’s array_multisort() Function to Sort Multiple Arrays

In the PHP function library, the array_multisort() function is a very useful function that can sort multiple arrays according to specified criteria. This article introduces how to use array_multisort() .

1. Syntax of array_multisort()

array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... [, mixed $... ]]]] ) : bool

Parameter description:

1. &$array1: Required parameter, the array to be sorted.

2. $array1_sort_order: Optional parameter, the sorting order for the first array. It can be SORT_ASC (ascending), SORT_DESC (descending), or left empty to use the default order.

3. $array1_sort_flags: Optional parameter, the sorting type used for the first array. It can be SORT_REGULAR , SORT_NUMERIC or SORT_STRING .

4. $... : Optional parameters, other arrays to be sorted.

2. Example demonstration of array_multisort() usage

Assume we want to sort two arrays, "names" and "ages", according to the students' "scores". The following code shows how to achieve this:

$names = array('Tom', 'Jack', 'Mike', 'John');
$ages = array('25', '18', '20', '22');
$scores = array('80', '60', '70', '90');
array_multisort($scores, SORT_DESC, SORT_NUMERIC, $names, $ages);

1. Explanation

In the code above we first define three arrays: $names , $ages and $scores .

Then we call array_multisort() to sort the $scores array (using SORT_DESC for descending order) and simultaneously pass the $names and $ages arrays so that they are reordered according to the sorted order of $scores .

Note: we also used the SORT_NUMERIC flag to specify that the sorting type for $scores is numeric.

2. Execution result

After running the code, we obtain the following result:

Array
(
    [0] => John
    [1] => Tom
    [2] => Mike
    [3] => Jack
)
Array
(
    [0] => 22
    [1] => 25
    [2] => 20
    [3] => 18
)

The analysis shows that the $scores ranking is 90, 80, 70, 60, and the corresponding names and ages are: John (22 years), Tom (25 years), Mike (20 years), Jack (18 years).

Conclusion

Through the above example we can see that the array_multisort() function is a very convenient tool that allows us to easily sort multiple arrays, making our work more efficient and convenient. When using it, we also need to pay attention to the correct usage of its parameters to avoid errors.

Java learning material download

C language learning material download

Frontend learning material download

C++ learning material download

PHP learning material download

backend developmentPHPCode Examplesortingphp-functionsarray_multisort
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.