In php, array_multisort () can sort multiple arrays at a time, or sort multi-dimensional arrays based on one or more dimensions. if the result is successful, TRUE is returned, and FALSE is returned if the result fails.
In php, array_multisort () can sort multiple arrays at a time, or sort multi-dimensional arrays based on one or more dimensions. if the result is successful, TRUE is returned, and FALSE is returned if the result fails.
Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])
If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.
Array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.
The association (string) key name remains unchanged, but the number key name is re-indexed.
Example 1. the code of the multi-dimensional array sorting instance is as follows:
- $ Ar = array (
- Array ("10", 11,100,100, ""),
- Array (1, 2, "2", 3, 1)
- );
- Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
- $ Ar [1], SORT_NUMERIC, SORT_DESC );
- Var_dump ($ ar );
- ?>
In this example, after sorting, the first array will transform to "10", 100,100, 11, "a" (it was sorted as strings in ascending order ). the second will contain in 1, 3, "2", 2, 1 (sorted as numbers, in descending order ).
The instance code is as follows:
- Array (2 ){
- [0] => array (5 ){
- [0] => string (2) "10"
- [1] => int (100)
- [2] => int (100)
- [3] => int (11)
- [4] => string (1) ""
- }
- [1] => array (5 ){
- [0] => int (1)
- [1] => int (3)
- [2] => string (1) "2"
- [3] => int (2)
- [4] => int (1)
- }
- }
In this example, after sorting, the first array will contain 10,100,100, "a" (as the string ascending order), and the second array will contain 1, 3, "2 ", 1 (sort by numerical descent ).
Example 2. the Sorting multi-dimen1_array instance code is as follows:
- $ Ar = array (
- Array ("10", 11,100,100, ""),
- Array (1, 2, "2", 3, 1)
- );
- Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
- $ Ar [1], SORT_NUMERIC, SORT_DESC );
- Var_dump ($ ar );
- ?>
In this example, after sorting, the first array will become "10", 100,100, 11, "a" (sorted in ascending order as strings ). the second array will contain 1, 3, "2", 2, 1 (sorted in descending order as numbers ).
The instance code is as follows:
- Array (2 ){
- [0] => array (5 ){
- [0] => string (2) "10"
- [1] => int (100)
- [2] => int (100)
- [3] => int (11)
- [4] => string (1) ""
- }
- [1] => array (5 ){
- [0] => int (1)
- [1] => int (3)
- [2] => string (1) "2"
- [3] => int (2)
- [4] => int (1)
- }
- }
The following is a common example code for an application based on instance 3:
- Header ('content-Type: text/html; charset = utf-8 ');
- Echo'
';
- // Original array format
- $ Array = array (
- 'Key1' => array (
- 'Item1' => '65 ',
- 'Item2' => '35 ',
- 'Item3' => '84 ',
- ),
- 'Key2' => array (
- 'Item1' => '24 ',
- ),
- 'Key3' => array (
- 'Item1' => '38 ',
- 'Item3' => '45 ',
- ),
- );
- // Key to be sorted
- // Sort by item1 in the array
- // You can also change to item2
- $ Sort = 'item1 ';
- Foreach ($ array as $ k => $ v)
- {
- $ NewArr [$ k] = $ v [$ sort];
- }
- // If this function is correctly executed, it will directly change the order of the original array key values
- // If the execution fails, bool (false) is returned)
- Array_multisort ($ newArr, SORT_DESC, $ array );
- Var_dump ($ array );
- // --------------------- The print effect of the sorted array starts --------------------
- Array (3 ){
- ["Key1"] =>
- Array (3 ){
- ["Item1"] =>
- String (2) "65"
- ["Item2"] =>
- String (2) "35"
- ["Item3"] =>
- String (2) "84"
- }
- ["Key3"] =>
- Array (2 ){
- ["Item1"] =>
- String (2) "38"
- ["Item3"] =>
- String (2) "45"
- }
- ["Key2"] =>
- Array (1 ){
- ["Item1"] =>
- String (2) "24"
- }
- }
- // --------------------- The print effect of the sorted array is over ---------------------