PHP sorts two-dimensional arrays by column

Source: Internet
Author: User
This article mainly introduces PHP's method of sorting two-dimensional arrays by a column, and analyzes the php's two-dimensional array sorting technique in the form of examples, involving the use of the array_multisort function, for more information about how to sort two-dimensional arrays by column in PHP, see the following example. We will share this with you for your reference. The details are as follows:

/** Sort two-dimensional arrays by column * array_multisort ($ arr1, $ arr2) * example of the manual **/$ data [] = array ('Volume '=> 67, 'version' => 2); $ data [] = array ('Volume '=> 86, 'version' => 1 ); $ data [] = array ('Volume '=> 85, 'version' => 6); $ data [] = array ('Volume' => 98, 'version' => 2); $ data [] = array ('Volume '=> 86, 'version' => 6 ); $ data [] = array ('Volume '=> 67, 'version' => 7); // get the column list foreach ($ data as $ key => $ row) {$ volume [$ key] = $ row ['Volume ']; $ edition [$ key] = $ row ['version'];} // sort data by volume in descending order and edition in ascending order // use $ data as the last parameter and sort array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data ); /*************************************// * as shown in the preceding example, array_multisort () the function * sorts every two-dimensional array column as a parameter * to sort the entire array *//**************** * *******************/function sigcol_arrsort ($ data, $ col, $ type = SORT_DESC) {if (is_array ($ data) {$ I = 0; foreach ($ data as $ k => $ v) {if (key_exists ($ col, $ v) {$ arr [$ I] = $ v [$ col]; $ I ++ ;}else {continue ;}}} else {return false;} array_multisort ($ arr, $ type, $ data); return $ data;} print_r (sigcol_arrsort ($ data, 'version', SORT_DESC )); /* to put it bluntly, the new array's original two-dimensional array 2 array ('Volume '=> 67, 'version' => 2); 1 array ('Volume' => 86, 'version' => 1); 6 array ('Volume '=> 85, 'version' => 6); 2 array ('Volume' => 98, 'version' => 2); 6 array ('Volume '=> 86, 'version' => 6); 7 array ('Volume' => 67, 'version' => 7); each row of the two arrays is locked together. Once the order of the new array changes, the two-dimensional arrays of each row are also changed, that is, the new array is equivalent to the order */

Running result:

Array(  [0] => Array    (      [volume] => 67      [edition] => 7    )  [1] => Array    (      [volume] => 85      [edition] => 6    )  [2] => Array    (      [volume] => 86      [edition] => 6    )  [3] => Array    (      [volume] => 67      [edition] => 2    )  [4] => Array    (      [volume] => 98      [edition] => 2    )  [5] => Array    (      [volume] => 86      [edition] => 1    ))

I hope this article will help you with PHP programming.

For more articles about how to sort two-dimensional arrays by column in PHP, refer to PHP Chinese website!

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.