PHP implements a method of ordering a key in a two-dimensional array _php tips

Source: Internet
Author: User

The example in this article describes a method that PHP implements to sort a key in a two-dimensional array. Share to everyone for your reference, specific as follows:

/** * Sorting Query result set * @access public * @param array $list Query results * @param string $field sorted field name * @param string $sortby sort Type (ASC forward sort desc Reverse sort Nat natural Sort) * @return Array */function list_sort_by ($list, $field, $sortby = ' asc ') {if is_
      Array ($list)) {$refer = $resultSet = Array ();
      foreach ($list as $i => $data) {$refer [$i] = & $data [$field];
          Switch ($sortby) {case ' ASC '://forward sort asort ($refer);
        Break
          Case ' desc '://reverse order Arsort ($refer);
        Break
          Case ' NAT '://Natural sort natcasesort ($refer);
      Break
      foreach ($refer as $key => $val) {$resultSet [] = & $list [$key];
    return $resultSet;
  return false;
 /** * Example * ask: in descending order of the ID key value of a two-dimensional array (i.e., the larger the ID)? */$list = Array (0 => array (' ID ' => 1, ' name ' => ' first '), 1 => array (' ID ' => 3, ' NA Me ' => ' third '), 2=> Array (' ID ' => 2, ' name ' => ' second '), 3 => array (' ID ' => 4, ' name ' => ' four '),);
Solution $new _list = list_sort_by ($list, ' id ', ' desc ');

 Print_r ($new _list);

The results of the operation are as follows:

Array
(
  [0] => array
    (
      [id] => 4
      [name] => fourth
    )
  [1] => array
    (
      [id] = > 3
      [name] => third
    )
  [2] => Array
    (
      [id] => 2
      [name] => second
    )
  [3] =& Gt Array
    (
      [id] => 1
      [name] => first
    )
)

More interested in PHP related content readers can view the site topics: "PHP array" Operation Tips Encyclopedia, "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

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.