PHP array_multisort () using the method to introduce _php Foundation

Source: Internet
Author: User
Tags mixed

A function that uses the Array_multisort () function to sort multiple arrays or multidimensional arrays, and a friend who is studying array sorting and multidimensional array ordering can look at it.

function bool Array_multisort (array & $arr [, Mixed $arg = Sort_asc [, Mixed $arg = Sort_regular [, mixed $ ...]])

Parameter description: function to sort multiple arrays or multidimensional arrays

The first argument is an array, and each subsequent argument may be an array, or it may be the following sort order flag
SORT_ASC-Default, in ascending order
Sort_desc-In descending order
You can then specify the sort type
Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Arranges each item in numerical order.
Sort_string-Arranges each entry in alphabetical order.
Cases:

Copy Code code as follows:

<?php
$arr 1 = Array (' Ten ', one, one,,, ' a ');
$arr 2 = Array (1, 2, 3, ' 2 ', 5);
Array_multisort ($arr 1, $arr 2);
?>

The results are:
$arr 1
Array ([0] => [1] => a [2] => [3] => [4] => 100)
# ' 10 ' is converted to an integer 10 when compared with 11, 100, 100, and less than the other three numbers
# ' 10 ' is a ' a ' comparison as a string whose first character ' 1 ' has an ASCII code value of 49 less than ' a ' (ASCII value 97), so ' 10 ' is the smallest element
# ' A ' is converted to an integer 0, less than three other numbers when the other three digits are compared
$arr 2
Array ([0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3)
# $arr 2 element 1 corresponds to the $ARR1 element ' 10 ' position, so it's positioned at [0]
# $arr 1[2] =>, $arr 1[3] => 100 correspond to $ARR2 elements 3, ' 2 ' respectively. 3 is greater than ' 2 ', so with 2 corresponding $arr1[2] => 100 sorted subscript 3, with 3 corresponding $arr1[3] => 100 sorted subscript 4
Summary----------
1. The number of array elements involved in sorting remains consistent
2. Sort array element position corresponding to, ' Ten ' => 1, one => 2
3. The array is sorted based on the order of the preceding array
4. The preceding array, when equal elements are compared, compares the rear array







Below is the example used, the manual example is not listed, on my own work in a few.

The most likely
work is a two-dimensional array, and you want to try a three-dimensional array, but think about it or forget it.


Copy Code code as follows:

header (' content-type:text/html; Charset=utf-8 ');
Echo ' <pre> ';
//original array format
$array = Array (
' key1 ' => array (
' item1 ' => '),
' item2 ' => ',
' it Em3 ' => ',
),
' Key2 ' => array (
' item1 ' => ',
),
' Key3 ' => array (
' it Em1 ' => ',
' item3 ' => ',
',
);
///To sort the keys
//According to the item1 in the array
//You can also change to item2
$sort = ' item1 ';
foreach ($array as $k => $v)
{
$newArr [$k] = $v [$sort];
}
///This function will directly change the order of the original array key values
//If execution fails, then he will return bool (false)
Array_multisort ($NEWARR, Sort_desc, $array) ;
Var_dump ($array); The
//---------------------the array print effect after sorting begins--------------------
Array (3) {
["Key1"]=>
Array (3) {
] "Item1"]=>
String (2) ""
["item2"]=>
String (2) ""
["Item3"]=>
String (2) "84" br>}
[Key3]]=>
Array (2) {
["item1"]=>
String (2) ""
["Item3"]=>
String (2) "" "
}
[" Key2 "]=>
Array (1) {
[" item1 "]=>
String (2)"
}
}
//---------------------sorted array The print effect ends---------------------



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.