Phparray_multisort () multi-group array sorting method _ PHP Tutorial

Source: Internet
Author: User
Phparray_multisort () multi-group array sorting method. 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. sort multidimensional arrays

The 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 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 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 the values in descending order ).

Example 2. Sorting multi-dimen1_array

The 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 (arranged in descending order as numbers ).

The 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)
}
}

Instance 3 provides a comprehensive implementation of common instances in applications.

The code is as follows:

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 ---------------------

For details about the array_multisort () function, refer to http://www.bKjia. c0m/phper/php-function/39192.htm

Sort () can sort multiple arrays at a time, or sort multi-dimensional arrays by one or more dimensions. if the result is successful, TRUE is returned, and FALSE is returned if the result is failed ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.