array_multisort() function in PHP

PHPz
Release: 2023-09-10 06:00:01
forward
1149 people have browsed it

array_multisort() function in PHP

array_multisort() function is used to sort multiple or multi-dimensional arrays. It returns a sorted array.

Syntax

array_multisort(arr1, sort_order, sort_type, arr2, arr3, arr4...)
Copy after login

Parameters

  • arr1 − Array to be sorted

  • sort_order − Sort order. The following are possible values

    • - SORT_ASC - Default. Sort in ascending order (A-Z)

    • - SORT_DESC - Sort in descending order (Z-A)

  • sort_type − Sorting behavior. The following are possible values ​​

    • SORT_REGULAR - Default. Compare elements in the normal way (standard ASCII)

    • SORT_NUMERIC - Compare elements as numeric values ​​

    • SORT_STRING - Compare elements as strings

    • SORT_LOCALE_STRING - Compares elements as strings, based on the current locale (can be changed using setlocale())

    • SORT_NATURAL - Use "Natural sorting" compares elements as strings, similar to natsort()

    • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL for case-insensitivity String sorting.

  • arr2 − Another array. Optional

  • arr3 − Another array. Optional.

  • arr4 − Another array. Optional.

Return value

array_multisort() function returns a sorted array.

Example

Demonstration

<?php
$a1 = array(12, 55, 3, 9, 99);
$a2 = array(44, 67, 22, 78, 46);
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Copy after login

Output

Array
(
[0] => 3
[1] => 9
[2] => 12
[3] => 55
[4] => 99
)
Array
(
[0] => 22
[1] => 78
[2] => 44
[3] => 67
[4] => 46
)
Copy after login

Example

Let us see another example of merging two arrays and sorting them in ascending order An example.

Online demonstration

<?php
$a1 = array(12, 55, 3, 9, 99);
$a2 = array(44, 67, 22, 78, 46);
$num = array_merge($a1,$a2);
array_multisort($num,SORT_ASC,SORT_NUMERIC);
print_r($num);
?>
Copy after login

Output

Array
(
[0] => 3
[1] => 9
[2] => 12
[3] => 22
[4] => 44
[5] => 46
[6] => 55
[7] => 67
[8] => 78
[9] => 99
)
Copy after login

The above is the detailed content of array_multisort() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template