Array_multisort () sorting description

Source: Internet
Author: User

The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order signs (the sort mark is used to change the default sort order:

Sort_asc-default, in ascending order. (A-Z) sort_desc-sort by descending order. (Z-A) You can then specify the sort type:

Sort_regular-default. Sort each item in the general order. Sort_numeric-Sort each item in numerical order. Sort_string-Sort each item in alphabetical order.Syntax

Array_multisort (array1, sorting order, sorting type, array2, array3 ...)
Parameters Description
Array1 Required. Specifies the input array.
Sorting order Optional. Specify the order. Possible values are sort_asc and sort_desc.
Sorting type Optional. Specifies the sorting type. Possible values are sort_regular, sort_numeric, and sort_string.
Array2 Optional. Specifies the input array.
Array3 Optional. Specifies the input array.
Edit this section Tips and comments

Note: The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1.

Note: You can set the sorting order and type behind each array. If no value is set, the default value is used for each array parameter.

Edit this section Example 1

<? PHP $ a1 = array ("dog", "cat"); $ A2 = array ("Fido", "missy ");
Array_multisort ($ A1, $ A2); print_r ($ A1); print_r ($ A2 );
?>

Output:

Array ([0] => CAT [1] => dog) array ([0] => Missy [1] => Fido)
Edit this section Example 2

How to sort two values at the same time:

<? PHP $ a1 = array ("dog", "dog", "cat ");
$ A2 = array ("Pluto", "Fido", "missy"); array_multisort ($ A1, $ A2 );
Print_r ($ A1); print_r ($ A2);?>

Output:

Array ([0] => CAT [1] => dog [2] => dog) array ([0] => Missy [1] => Fido [2] => Pluto)
Edit this section Example 3

With sorting parameters:

<? PHP $ a1 = array ("dog", "dog", "cat ");
$ A2 = array ("Pluto", "Fido", "missy ");
Array_multisort ($ A1, sort_asc, $ A2, sort_desc); print_r ($ A1 );
Print_r ($ A2);?>

Output:

Array ([0] => CAT [1] => dog [2] => dog) array ([0] => Missy [1] => Pluto [2] => Fido)
Edit this section Instance 4

Multi-dimensional array sorting [1]

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 it to item2 $ sort = 'item1 '; foreach ($ array as $ K =>$ v) {$ newarr [$ K] = $ V [$ sort];}
// if this function is correctly executed, it will directly change the sequence of the original array key values. // if the execution fails, it will return bool (false)
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" }}< br> // --------------------- the print effect of the sorted array is over ---------------------

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.