Home  >  Article  >  Backend Development  >  Introduction to how to use array_multisort() in php_PHP tutorial

Introduction to how to use array_multisort() in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:44995browse

Use the array_multisort() function to sort multiple arrays or multi-dimensional arrays. Friends who are studying array sorting and multi-dimensional array sorting can take a look.

Function bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] )

Parameter description: The function sorts multiple arrays or multi-dimensional arrays

The first parameter is an array, each subsequent parameter may be an array or the following sort order flags
SORT_ASC - Default, sort in ascending order
SORT_DESC - Sort in descending order
Subsequently can Specify the type of sort
SORT_REGULAR - Default. Arrange each item in regular order.
SORT_NUMERIC - Sort each item in numerical order.
SORT_STRING - Sort each item in alphabetical order.
Example:

Copy code The code is as follows:

$arr1 = array('10', 11, 100, 100, 'a');
$arr2 = array(1, 2, 3, '2', 5);
array_multisort($arr1, $arr2);
?>

The result is:
$arr1
Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 )
# '10' is converted to the integer 10 when compared with 11, 100, 100, which is smaller than the other three numbers
# '10' is used as a string when compared with 'a', its first character '1' ascii code value is 49 which is less than 'a' (ascii value is 97), so '10' is the smallest element
# 'a' is converted to an integer 0 when comparing the other three numbers, which is less than the other three Number
$arr2
Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3 )
# $arr2 element 1 corresponds to $arr1 element '10' position, so it is ranked at [0] position
# $arr1[2] => 100, $arr1[3] => 100 respectively correspond to $arr2 elements 3, '2'. 3 is greater than '2', so the $arr1[2] corresponding to 2 => 100 has the sorted subscript 3, and the $arr1[3] corresponding to 3 => 100 has the sorted index 4
Summary----------
1. The number of array elements involved in sorting remains the same
2. The positions of the sorted array elements correspond to, for example, '10' => 1 , 11 => 2
3. The following array is sorted based on the order of the previous array
4. If the previous array encounters equal elements, the following array is compared



The following is an example of use, I won’t list the examples in the manual, just a few from my own work.
Probably the two-dimensional array that I come across the most in my work is a three-dimensional array. I wanted to try a three-dimensional array, but after thinking about it, I decided not to do it.
Copy code 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',
),
);
//Keys to be sorted
//Sort according to item1 in the array
//You can also change to item2
$sort = 'item1' ;
foreach($array as $k => $v)
{
$newArr[$k] = $v[$sort];
}
//This function if If executed correctly, it will directly change the order of the original array key values ​​
//If the execution fails, then it will return bool(false)
array_multisort($newArr,SORT_DESC, $array);
var_dump($array );
//---------------------The sorted array printing effect begins--------------- -----
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 sorted array printing effect ends----- ----------------

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325455.htmlTechArticleUse the array_multisort() function to sort multiple arrays or multi-dimensional arrays. We are studying array sorting and multi-dimensional arrays. Friends who sort can take a look. Function bool array_multisor...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn