PHP array_multisort () function details

Source: Internet
Author: User
I. first look at the simplest situation. There are two Arrays:  $ 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:  $ Arr1 = Array (1, 9, 5 );  $ Arr2 = Array (6, 2, 4 );  $ Arr3 = Array (3, 7, 8);  Array_multisort ( $ Arr1 , $ Arr2 , $ Arr3  ); Check the result. 1 corresponds to 6 corresponding to 3 from start to end, and so does 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. Corresponding to 1 , 6, 3 is a data row, 9, 2, 7 is 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:  $ Arr1 = Array (, );  $ Arr2 = Array (6, 2, 4, 1);  $ Arr3 = Array (3, 7, 8, 0 );  Array_multisort ( $ Arr1 , $ Arr2 , $ Arr3  ); As you can imagine  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. $ Arr2 in descending order  And compared as a string, it must be written:  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. Sort order mark: sort_asc - Sort by Ascending Order (default) sort_desc - Sort_regular - Compare projects by the common method (default) sort_numeric -Compare sort_string by value - Compare the project by string. 3. What is the actual function of array_multisort. We usually have some multidimensional arrays that need to be sorted:  $ Guys = Array  ([ 0] => Array  ([Name] => Jake [score] => 80 [Grade] => A )[ 1] => Array  ([Name] =>Jin [score] => 70 [Grade] => A )[ 2] => Array  ([Name] => John [score] => 80 [Grade] => A )[ 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  $ Guys get two more arrays in sequence  :  $ Scores = Array (80, 70, 80, 20 );  $ Names = Array ('Jar', 'jin', 'john', 'ben' ); Then  Array_multisort ( $ Scores , Sort_desc, $ Names ,$ Guys  ); Can I make it more flexible? Do I have to get some other arrays every time I want to sort them? 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:  /* ** Sort arrays by specified keys ** 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 ('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 $ keyname the sort key * @ Param int $ dir sort 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 an SQL statement: * @ code PHP * $ rows = helper_array: sortbymulticols ($ rows, array (* 'parent' => sort_asc, * 'name' => sort_desc ,*)); * @ endcode ** @ Param array $ array to be sorted * @ Param array $ ARGs sort key ** @ return array 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  ;} 

 

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.