Example of how to use the php custom two-dimensional array sorting function array_orderby,

Source: Internet
Author: User

Example of how to use the php custom two-dimensional array sorting function array_orderby,

This article describes the usage of the php custom two-dimensional array sorting function array_orderby. We will share this with you for your reference. The details are as follows:

<?php/**I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort(). */function array_orderby(){  $args = func_get_args();  $data = array_shift($args);  foreach ($args as $n => $field) {    if (is_string($field)) {      $tmp = array();      foreach ($data as $key => $row)        $tmp[$key] = $row[$field];      $args[$n] = $tmp;      }  }  $args[] = &$data;  call_user_func_array('array_multisort', $args);  return array_pop($args);}/*The sorted array is now in the return value of the function instead of being passed by reference.*/$data[] = array('volume' => 67, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 1);$data[] = array('volume' => 85, 'edition' => 6);$data[] = array('volume' => 98, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 6);$data[] = array('volume' => 67, 'edition' => 7);// Pass the array, followed by the column names and sort flags$sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);print_r($sorted)?>

Running result:

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

PS: here we recommend a demo tool for sorting for your reference:

Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys

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.