PHP two-dimensional array sorting

Source: Internet
Author: User
PHP itself is a function that has a multidimensional array ordering.

BOOL Array_multisort (array $ar 1 [, Mixed $arg [, mixed $ ... [, Array $ ...]] )

The following is a description of the Array_multisort function in the manual:

Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
The association (string) key name remains the same, but the numeric key name is re-indexed.
The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on.

As you can see from the manual, the multidimensional ordering of PHP itself is sorted by the first array, and the subsequent order is adjusted. Arrays like this:

Array (' id ' = = Array (1,3,2),

' Data ' =>array (' A ', ' C ', ' B '))

As long as the ID of the multidimensional sort, you can. But most of the time, the array we construct is this:

Array
Array (' ID ' =>1, ' data ' = ' a '),
Array (' ID ' =>3, ' data ' = ' C '),
Array (' ID ' =>2, ' data ' = ' B ')
);

The elements of the array are arranged in rows and need to be sorted by one of the columns. PHP does not seem to provide a function like a matrix transpose, so it is not possible to use Array_multisort directly for multidimensional sorting. However, you just need to extract the sorted columns first, and pass them as the first parameter to Array_multisort.

function Multi_array_sort ($multi _array, $sort _key, $sort =sort_asc) {      if (Is_array ($multi _array)) {          foreach ($ Multi_array as $row _array) {              if (Is_array ($row _array)) {                  $key _array[] = $row _array[$sort _key];              } else{                  return-1;}}}      else{          return-1;      }      Array_multisort ($key _array, $sort, $multi _array);      return $multi _array;  }
  • 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.