PHP _php Techniques for implementing two-dimensional arrays sorted by a column

Source: Internet
Author: User
This article mainly introduces PHP implementation of a two-dimensional array by a column to sort the method, combined with an example of PHP two-dimensional array sequencing techniques, involving the use of Array_multisort functions, the need for friends can refer to the next

Specific as follows:

/** a two-dimensional array sorted by a column * Array_multisort ($arr 1, $arr 2) * The manual example is as follows **/$data [] = Array (' volume ' = ' + ', ' edition ' = 2); $data [] = AR Ray (' volume ' = +, ' edition ' + 1); $data [] = Array (' volume ' = = ' Edition ' = 6); $data [] = Array (' volume ' = > 98, ' Edition ' + 2); $data [] = Array (' volume ' = +, ' Edition ' = + 6); $data [] = Array (' volume ' = ' + ', ' edit  Ion ' = + 7);//Gets the list of columns foreach ($data as $key + $row) {$volume [$key] = $row [' volume ']; $edition [$key] = $row [' edition '];} Sort the data in descending order of volume, in ascending order according to edition//$data as the last parameter, sorted with Universal key Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $dat a);/*************************************//* from the above example, it is not difficult to see that the Array_multisort () function * is to sort the columns of each two-dimensional array as a parameter * to achieve the ordering of 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 (The Sigcol_arrsort ($data, ' edition ', Sort_desc));/* The principle is: The new array original two-dimensional array 2 array (' volume ' = +, ' edition ' = 2); 1 Array (' volume ' = +, ' Edition ' = + 1); 6 Array (' Volume ' = ' Edition ' = 6); 2 Array (' volume ' = = 98, ' edition ' = 2); 6 Array (' volume ' = +, ' Edition ' = + 6); 7 Array (' volume ' = +, ' Edition ' = + 7); Each row of the two arrays is locked together, and once the new array is in the order of change, then the two-dimensional array of each row is followed by the transformation order, that is, the new array is the reference to order B in the query clause y*/

Operation Result:

Array (  [0] = = Array    (      [Volume] = [      Edition] = 7    )  [1] = = Array    (      [ Volume] =      [Edition] + 6    )  [2] = = Array    (      [Volume] = [      Edition] = 6< c14/>)  [3] = = Array    (      [Volume] = [      Edition] = 2    )  [4] = = Array    (      [Volume] = 98      [Edition] + 2    )  [5] = =    Array      ([volume] = [      Edition] = 1    ))

The above is the whole content of this article, I hope that everyone's study has helped.


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.