PHP implements a two-dimensional array to sort by a column _php tips

Source: Internet
Author: User
Tags exception handling php error mysql database

The example in this article tells you how PHP implements a two-dimensional array to sort by a column. Share to everyone for your reference, specific as follows:

* * The two-dimensional array is sorted by a column * Array_multisort ($arr 1, $arr 2) * Manual examples are as follows */$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' edition ' => 1);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' edition ' => 7);
  Gets the list of columns foreach ($data as $key => $row) {$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition ']; ///volume The data in descending order according to edition ascending order//$data as the last parameter, sorted by common key Array_multisort ($volume, Sort_desc, $edition, SORT_ASC,
$DATA); /*************************************//* From the example above, it is easy to see that the Array_multisort () function * is to order the columns of each two-dimensional array as a parameter * to achieve the order 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 (Sigcol_arrsort ($data, ' edition ', Sort_desc));
 /* The principle is: The new array of the original two-dimensional array 2 array (' Volume ' =>, ' Edition ' => 2);
 1 Array (' volume ' =>, ' edition ' => 1);
 6 Array (' Volume ' =>, ' Edition ' => 6);
 2 Array (' Volume ' =>, ' 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 order, the two-dimensional array of each row is followed by the order of the transformation, that is, the new array is the same as the reference in the query clause.

Run Result:

Array
(
  [0] => array
    (
      [volume] =>
      [Edition] => 7
    )
  [1] => array
    (
      [Volume] =>
      [Edition] => 6
  [2] => Array
    (
      [Volume] => (
      edition) => 6
    )
  [3] => array
    (
      [volume] =>
      [Edition] => 2
    )
  [4] => array
    ( C24/>[volume] =>
      [Edition] => 2
  [5] => Array
    (
      [Volume] => (
      edition) => 1
    )
)

More interested in PHP related content readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP string (String) Usage summary", "PHP for XML file Operating skills summary", " PHP error and exception handling method summary, "PHP operation and operator Usage Summary", "PHP basic Grammar Introductory Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database Operation Introduction Course" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.