Php two-dimensional array sorting methods

Source: Internet
Author: User
Two-dimensional array sorting also provides a function array_multisort in php to directly sort the data, in addition to using array_multisort to sort arrays, we have also written some custom two-dimensional array sorting methods .... Two-dimensional array sorting also provides a function array_multisort in php to directly sort the data, in addition to using array_multisort to sort arrays, we have also written some custom two-dimensional array sorting methods.

Sometimes to achieve a certain purpose, we need to sort the two-dimensional array. now let's share the implementation method.

 Array ('date' => '2014-08-18 ', 'num' => 5), '2' => array ('date' => '2014-08-20 ', 'num' => 3), '3' => array ('date' => '2017-08-17 ', 'num' => 10 )); $ result = sysSortArray ($ arr, 'num'); // the effect after running is as follows: $ arr = array ('1' => array ('date' => '2017-08-18 ', 'num' => 3 ), '2' => array ('date' => '2017-08-20 ', 'num' => 5 ), '3' => array ('date' => '2017-08-17 ', 'num' => 10); // functions used: /*** @ package two-dimensional count Group sorting * @ version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $ *** Sort an two-dimension array by some level two items use array_multisort () function. ** sysSortArray ($ Array, "Key1", "SORT_ASC", "SORT_RETULAR", "Key2 ";......) * @ Author lamp100 * @ param array $ ArrayData the array to sort. * @ param string $ KeyName1 the first item to sort. * @ param string $ SortOrder1 the order to sort by ("SORT_ASC" | "SORT_DESC ") * @ param string $ SortType1 the sort type ("SORT_REGULAR" | "SORT_NUMERIC" | "SORT_STRING") * @ return array sorted array. */function sysSortArray ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REG ULAR ") {if (! Is_array ($ ArrayData) {return $ ArrayData;} // Get args number. $ ArgCount = func_num_args (); // Get keys to sort by and put them to SortRule array. for ($ I = 1; $ I <$ ArgCount; $ I ++) {$ Arg = func_get_arg ($ I); if (! Eregi ("SORT", $ Arg) {$ KeyNameList [] = $ Arg; $ SortRule [] = '$ '. $ Arg;} else {$ SortRule [] = $ Arg ;}// Get the values according to the keys and put them to array. foreach ($ ArrayData AS $ Key => $ Info) {foreach ($ KeyNameList AS $ KeyName) {$ KeyName} [$ Key] = $ Info [$ KeyName] ;}// Create the eval string and eval it. $ EvalString = 'Array _ multisort ('. join (",", $ SortRule ). ', $ ArrayData ); '; Eval ($ EvalString); return $ ArrayData ;}?>

In addition, the array_multisort function is also very powerful. For more information, see The PHP Manual.


We can use the array_multisort () function. The array_multisort () function sorts multiple arrays or multi-dimensional arrays.

The array in the parameter is treated as a table column and sorted BY rows-similar to the SQL ORDER BY clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.

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 sorting type:

• SORT_REGULAR-default. Sort each item in the general order.

• SORT_NUMERIC-sort each item in numerical order.

• SORT_STRING-sort each item alphabetically.

Syntax: array_multisort (array1, sorting order, sorting type, array2, array3 ...)

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

The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1. 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.

The following is an example:

 '; For ($ I = 0; $ I <= 5; $ I ++) {$ arr [$ I] ['Val'] = mt_rand (1,100 ); $ arr [$ I] ['num'] = mt_rand (1,100);} echo'
';print_r($arr);echo '
'; Echo' extract the key from the two-dimensional array as val and separate it into another array :'.'
'; Foreach ($ arr as $ key => $ row) {$ vals [$ key] = $ row ['Val']; $ nums [$ key] = $ row ['num'];} echo'
';print_r($vals);echo '
'; Echo :'.'
'; Array_multisort ($ vals, SORT_ASC, $ arr); echo'
';print_r($vals);echo '
';?>

Running result:

Two-dimensional Array: Array ([0] => Array ([val] => 46 [num] => 49) [1] => Array ([val] => 8 [num] => 24) [2] => Array ([val] => 37 [num] => 3) [3] => Array ([val] => 32 [num] => 35) [4] => Array ([val] => 19 [num] => 38) [5] => Array ([val] => 30 [num] => 37) extract the key from the two-dimensional Array as val and separate it into another Array: array ([0] => 46 [1] => 8 [2] => 37 [3] => 32 [4] => 19 [5] => 30) sort it: array ([0] => 8 [1] => 19 [2] => 30 [3] => 32 [4] => 37 [5] => 46)

We will get a two-dimensional array in ascending order of val.


Tutorial address:

Reprinted! But please include the article address ^

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.