Php instructions for array_multisort multi-dimensional array sorting _ PHP Tutorial

Source: Internet
Author: User
Php instructions for array_multisort multi-dimensional array sorting. I. first look at the simplest situation. There are two arrays: Copy the code as follows: $ arr1array (, 5); $ arr2array (, 4); array_multisort ($ arr1, $ arr2); print_r ($ arr1 ); returns I. first look at the simplest situation. There are two arrays:

The code is as follows:


$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );
Array_multisort ($ arr1, $ arr2 );
Print_r ($ arr1); // The order is, 9.
Print_r ($ arr2); // The order is 6, 4, 2.


I estimate that the values of the two arrays correspond from beginning to end: 1 corresponds to 6, 9 corresponds to 2, 5 corresponds to 4.
Let's add an array to see what will happen:

The code is as follows:


$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );
$ Arr3 = array (3, 7, 8 );
Array_multisort ($ arr1, $ arr2, $ arr3 );


View the result. 1 corresponds to 6 and 3 from start to end. this is also true for other items. This correspondence is the so-called "keep the original key-name association during sorting" in the manual ".
You can also think of each array as a column in a database table. The 1, 6, and 3 corresponding to a data row, 9, 2, and 7 are another data row...
Array_multisort will first sort by the first array (as expected). If the value of the first array (column) is the same, it will sort by the second array (column.
You can use the following program to test the function:

The code is as follows:


$ Arr1 = array (1, 9, 5, 9 );
$ Arr2 = array (6, 2, 4, 1 );
$ Arr3 = array (3, 7, 8, 0 );
Array_multisort ($ arr1, $ arr2, $ arr3 );


The result of $ arr3 is ).
II. The following describes the parameters of array_multisort.The parameters of this function are flexible. The simplest case is that one or n arrays are used as parameters as shown above. Note that the number of items in each array must be the same; otherwise, the sorting will fail due to warning.
Array_multisort ($ arr1, $ arr2, $ arr3); by default, all arrays are sorted in ascending order. if you want to sort $ arr2 in descending order and compare it as a string, you must write it as follows:
Array_multisort ($ arr1, $ arr2, SORT_DESC, SORT_STRING, $ arr3 );
Each array can be followed by a sort order sign or a sort type sign, or both. However, each sort flag can only appear after each array.
The details are as follows:
Sort order mark:

SORT_ASC-sort by ascending order (default)
SORT_DESC-sort by descent

Sorting type flag:

SORT_REGULAR-compare projects by the common method (default)
SORT_NUMERIC-compare projects by numerical values
SORT_STRING-compare items by string

3. What is the role of array_multisort.
We usually have some multidimensional arrays that need to be sorted:
$ Guys = Array
(
[0] => Array
(
[Name] => jake
[Score] => 80
[Grade] =>
)
[1] => Array
(
[Name] => jin
[Score] => 70
[Grade] =>
)
[2] => Array
(
[Name] => john
[Score] => 80
[Grade] =>
)
[3] => Array
(
[Name] => ben
[Score] => 20
[Grade] => B
)
)
For example, we want to sort the scores in descending order. if the scores are the same, they are listed in ascending order by name. In this case, we need to get two more arrays in the order of $ guys: $ scores = array (80, 70, 80, 20); $ names = array ('jar', 'Jin ', 'John', 'Ben'); then array_multisort ($ scores, SORT_DESC, $ names, $ guys); can we be more flexible, do I need to get another array for sorting? In fact, qeephp's helper_array class has encapsulated well. The following are two methods of qeephp. you need to modify them yourself to use them:

The code is as follows:


/**
* Sort the array by the specified key pair
*
* Usage:
* @ Code php
* $ Rows = array (
* Array ('id' => 1, 'value' => '1-1', 'parent' => 1 ),
* Array ('id' => 2, 'value' => '2-1', 'parent' => 1 ),
* Array ('id' => 3, 'value' => '3-1', 'parent' => 1 ),
* Array ('id' => 4, 'value' => '4-1', 'parent' => 2 ),
* Array ('id' => 5, 'value' => '5-1', 'parent' => 2 ),
* Array ('id' => 6, 'value' => '6-1', 'parent' => 3 ),
*);
*
* $ Rows = Helper_Array: sortByCol ($ rows, 'id', SORT_DESC );
* Dump ($ rows );
* // The output result is:
* // Array (
* // Array ('id' => 6, 'value' => '6-1', 'parent' => 3 ),
* // Array ('id' => 5, 'value' => '5-1', 'parent' => 2 ),
* // Array ('id' => 4, 'value' => '4-1', 'parent' => 2 ),
* // Array ('id' => 3, 'value' => '3-1', 'parent' => 1 ),
* // Array ('id' => 2, 'value' => '2-1', 'parent' => 1 ),
* // Array ('id' => 1, 'value' => '1-1', 'parent' => 1 ),
*//)
* @ Endcode
*
* @ Param array $ array the array to be sorted
* @ Param string $ key for sorting by keyname
* @ Param int $ dir sorting direction
*
* @ Return array the sorted array
*/
Static function sortByCol ($ array, $ keyname, $ dir = SORT_ASC)
{
Return self: sortByMultiCols ($ array, array ($ keyname => $ dir ));
}/**
* Sort a two-dimensional array BY multiple columns, similar to order by in SQL statements.
*
* Usage:
* @ Code php
* $ Rows = Helper_Array: sortByMultiCols ($ rows, array (
* 'Parent' => SORT_ASC,
* 'Name' => SORT_DESC,
*));
* @ Endcode
*
* @ Param array $ array to be sorted by rowset
* @ Param array $ args sort key
*
* @ Return array the sorted array
*/
Static function sortByMultiCols ($ rowset, $ args)
{
$ SortArray = array ();
$ SortRule = '';
Foreach ($ args as $ sortField => $ sortDir)
{
Foreach ($ rowset as $ offset => $ row)
{
$ SortArray [$ sortField] [$ offset] = $ row [$ sortField];
}
$ SortRule. = '$ sortArray [\ ''. $ sortField.' \ '],'. $ sortDir .',';
}
If (empty ($ sortArray) | empty ($ sortRule) {return $ rowset ;}
Eval ('Array _ multisort ('. $ sortRule.' $ rowset );');
Return $ rowset;
}


Bytes. There are two arrays: Code: $ arr1 = array (, 5); $ arr2 = array (, 4); array_multisort ($ arr1, $ arr2 ); print_r ($ arr1); // The result is...

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.