Home > Article > Backend Development > How to implement two-dimensional array sorting in php
In PHP, you can use the array_multisort() function to implement two-dimensional array sorting. This function can sort multiple arrays or multi-dimensional arrays, the syntax is "array_multisort (two-dimensional array, sort order, sort type)"; when the second parameter is omitted or set to "SORT_ASC", it will be sorted in ascending order and set to "SORT_DESC" Then sort in descending order.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use array_multisort () function implements two-dimensional array sorting.
<?php header("content-type:text/html;charset=utf-8"); $arr = array(5,3,array(2,4),1,array(3,6,1,0),-1); echo "原二维数组:"; var_dump($arr); echo "排序后的二维数组:"; array_multisort($arr); var_dump($arr); ?>
This function sorts the outer elements first, and then sorts the inner subarray.
Description:
php built-in function array_multisort() is used to sort multiple arrays or multi-dimensional arrays. (Modify the original array)
You can enter one or more arrays. The function sorts the first array first, then the other arrays, and if two or more values are the same, it sorts the next array.
array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters | Description |
---|---|
array1 | Required . Specifies an array. |
sorting order | Optional. Specify the order of sorting. Possible values:
|
sorting type | Optional. Specifies the sorting type. Possible values:
|
array2 | Optional. Specifies an array. |
array3 | Optional. Specifies an array. |
#Note: String key names will be preserved, but numeric key names will be re-indexed, starting at 0 and increasing by 1.
Return value: TRUE if successful, FALSE if failed.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement two-dimensional array sorting in php. For more information, please follow other related articles on the PHP Chinese website!