PHP two-dimensional array sorted by a field (sort the query result set)

Source: Internet
Author: User
We often run into a sort of a key value based on a two-dimensional array, and then suddenly think of a function in the Onethink project, which is then drawn out as a reference.

2014-05-22 17::15 Read the comments of enthusiastic phper to add the following:
PHP native Array_multisort () functions are recommended for faster execution and less reliance on custom functions
The official document explanation is difficult to understand, the friend who does not understand can use
Plain English explanation (Baidu know): Http://zhidao.baidu.com/link?url=Ljv-21fnK2CZkd03nPxb7uB7owjApdWilxZlmCcZKQqTB5AeI_ Bsdhyceiaa5gwl3o9xj2wtx8m65avjfmr9ck
  1. /**
  2. * Sort the query result set
  3. * http://www.onethink.cn
  4. */application/common/common/function.php
  5. *
  6. * @access Public
  7. * @param array $list Query results
  8. * @param string $field sorted field names
  9. * @param string $sortby Sort type (ASC forward sort desc Reverse sort Nat natural Sort)
  10. * @return Array
  11. */
  12. if (! function_exists (' list_sort_by '))
  13. {
  14. function list_sort_by ($list, $field, $sortby = ' ASC ')
  15. {
  16. if (Is_array ($list))
  17. {
  18. $refer = $resultSet = Array ();
  19. foreach ($list as $i = $data)
  20. {
  21. $refer [$i] = & $data [$field];
  22. }
  23. Switch ($sortby)
  24. {
  25. Case ' ASC '://forward sort
  26. Asort ($refer);
  27. Break
  28. Case ' desc '://Reverse sort
  29. Arsort ($refer);
  30. Break
  31. Case ' NAT '://Natural sort
  32. Natcasesort ($refer);
  33. Break
  34. }
  35. foreach ($refer as $key = $val)
  36. {
  37. $resultSet [] = & $list [$key];
  38. }
  39. return $resultSet;
  40. }
  41. return false;
  42. }
  43. }
Copy Code
    1. /**
    2. * Example
    3. * Request: According to the ID key values of the two-dimensional array in descending order (that is, the higher the ID of the row before)?
    4. */
    5. $list = Array (
    6. 0 = Array (
    7. ' id ' = 1,
    8. ' Name ' = ' first '
    9. ),
    10. 1 = Array (
    11. ' id ' = 3,
    12. ' Name ' = ' third '
    13. ),
    14. 2 = Array (
    15. ' id ' = 2,
    16. ' Name ' = ' second '
    17. ),
    18. 3 = Array (
    19. ' id ' = 4,
    20. ' Name ' = ' IV '
    21. ),
    22. );
    23. Answer
    24. $new _list = list_sort_by ($list, ' id ', ' desc ');
Copy Code
  • Related Article

    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.