How to use the PHP array sorting function array_multisort to sort tables _ PHP Tutorial

Source: Internet
Author: User
Tags array sort
How to use the PHP array sorting function array_multisort to sort tables. What we bring to you today is the original form of a function: array_multisort ($ sortKeyArray, $ ascOrDesc, $ sortArray ), the first parameter in the PHP array sorting function array_multisort is to ensure that what we bring to you today isThe original form of the function is array_multisort ($ sortKeyArray, $ ascOrDesc, $ sortArray ), the first parameter in the PHP array sorting function array_multisort is the sort column array to maintain the correspondence between the array key values. The second parameter is a predefined constant, SORT_ASC-sort by ascending order, SORT_DESC-sort by descending order, and the third parameter is the array to be sorted. Another default parameter is the sort data type, which is skipped here. You can see the following example in detail.

 
 
  1. php
  2. class Storage
  3. {
  4. function getSellList()
  5. {
  6. global $db;
  7. $db->query("set names utf8");
  8. $db->query("select * from sold_record");
  9. while ($row=$db->get_array())
  10. {
  11. $array[]=$row;
  12. }
  13. $db->free();
  14. return $array;
  15. }
  16. }
  17. $storage=new Storage();
  18. $sellList=$storage->getSellList();
  19. foreach ($sellList as $key => $row)
  20. {
  21. $cust[$key] = $row['customer_id'];
  22. $prod[$key] = $row['product'];
  23. $pty[$key] = $row['ptype'];
  24. $sdt[$key]=$row['sell_date'];
  25. }
  26. $asdes="yes";
  27. if($_GET[up]=="yes")
  28. {
  29. $asdes="no";
  30. $sort=SORT_DESC;
  31. }
  32. elseif($_GET[up]=="no")
  33. {
  34. $asdes="yes";
  35. $sort=SORT_ASC;
  36. }
  37. switch ($_GET[order])
  38. {
  39. case "cname":
  40. array_multisort($cust,$sort,$sellList);
  41. break;
  42. case "product":
  43. array_multisort($prod,$sort,$sellList);
  44. break;
  45. case "ptype":
  46. array_multisort($pty,$sort,$sellList);
  47. break;
  48. case "date":
  49. array_multisort($sdt,$sort,$sellList);
  50. break;
  51. }
  52. ?>

The code above is the specific code of the PHP array sorting function array_multisort for table sorting.


The original form of the sort function is array_multisort ($ sortKeyArray, $ ascOrDesc, $ sortArray). The first parameter in the PHP array sort function array_multisort is to protect...

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.