Php two-dimensional array sorting by a field (sorting the query result set)

Source: Internet
Author: User
Php two-dimensional array sorting by a field (sorting the query result set)
We often encounter sorting by a key value of a two-dimensional array, and then suddenly think of a function in the onethink project, so it is extracted for reference.

: 15 I saw the enthusiastic phper comments and added the following content:
We recommend that you use the native array_multisort () function of php. the execution speed is faster and the dependency of custom functions is reduced.
It is hard to understand the official documentation and can be used by friends who do not understand it.
Big vernacular (Baidu know): http://zhidao.baidu.com/link? Url = Ljv-21fnK2CZkd03nPxb7uB7owjApdWilxZlmCcZKQqTB5AeI_BsdhyCEIaa5gWl3o9xJ2WtX8m65avJFmR9CK

  1. /**
  2. * Sort query result sets.
  3. * Http://www.onethink.cn
  4. */Application/Common/function. php
  5. *
  6. * @ Access public
  7. * @ Param array $ list query result
  8. * @ Param string $ field name of the field to be sorted
  9. * @ Param string $ sortby sorting type (asc positive sorting desc reverse sorting nat natural sorting)
  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 ': // positive sorting
  26. Asort ($ refer );
  27. Break;
  28. Case 'desc': // reverse sorting
  29. Arsort ($ refer );
  30. Break;
  31. Case 'Nat ': // natural sorting
  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. }

  1. /**
  2. * Example
  3. * Q: is the key value of the two-dimensional array sorted in descending order (that is, the bigger the id, the more advanced )?
  4. */
  5. $ List = array (
  6. 0 => array (
  7. 'Id' => 1,
  8. 'Name' => '1'
  9. ),
  10. 1 => array (
  11. 'Id' => 3,
  12. 'Name' => '3'
  13. ),
  14. 2 => array (
  15. 'Id' => 2,
  16. 'Name' => '2'
  17. ),
  18. 3 => array (
  19. 'Id' => 4,
  20. 'Name' => '4'
  21. ),
  22. );
  23. // Answer
  24. $ New_list = list_sort_by ($ list, 'id', 'desc ');

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.