How to use the PHP array sorting function array_multisort to sort the table _php tutorial

Source: Internet
Author: User
Tags array sort
What we are bringing to you today is aThe prototype of the function is Array_multisort ($sortKeyArray, $ascOrDesc, $sortArray), and the PHP array sort function Array_ The first parameter in Multisort is an array of sorted columns to be constructed in order to maintain the corresponding relationship of the array key values, the second parameter is a predefined constant, SORT_ASC-sorted by ascending order, Sort_desc-sorted by descending order, and the third parameter is the array to be sorted. There is also a default parameter that is sorted by the data type, which is skipped here. Take a look at the example below and it will be clear.

 
 
  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 above code is the PHP array sorting function array_multisort the specific code to sort the table.


http://www.bkjia.com/PHPjc/446380.html www.bkjia.com true http://www.bkjia.com/PHPjc/446380.html techarticle What we bring to you today is the prototype of a function is Array_multisort ($sortKeyArray, $ascOrDesc, $sortArray), and the PHP array sort function Array_ The first parameter in Multisort is to protect ...

  • 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.