Backend Development 5 min read

Using 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 concrete example that sorts student names, ages, and scores, and shows the resulting output while highlighting important flags and usage tips.

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

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

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 specifying the sorting order for the first array. It can be SORT_ASC (ascending), SORT_DESC (descending), or omitted (default order).

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

4. $... : Optional parameters for additional arrays to be sorted.

2. Example Demonstration of array_multisort() Usage

Suppose we want to sort the "names" and "ages" arrays according to students' "scores". The following code can be used:

$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 the SORT_DESC order), and we also pass $names and $ages so that they are reordered according to the sorted $scores array.

Note: We also use the SORT_NUMERIC flag to specify that $scores should be sorted numerically.

2. Execution Result

After executing 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 $scores ranks as 90, 80, 70, 60, and the corresponding names and ages are: John (22), Tom (25), Mike (20), Jack (18).

Conclusion

From the example, we can see that the array_multisort() function is very convenient for sorting multiple arrays, making work more efficient and convenient. When using it, we must pay attention to the correct usage of parameters to avoid errors.

PHP8 Video Tutorial

Scan the QR code to receive learning materials for free

backend developmentPHPTutorialsortingarray_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.