Sorts specified items of a two-dimensional array.

Source: Internet
Author: User
Sorts specified items of a two-dimensional array.

  1. /*
  2. $ Array_name: input array;
  3. $ Row_id: the items to be sorted by the array;
  4. $ Order_type: sorting method, ASC or DESC;
  5. */
  6. Function array_sort ($ array_name, $ row_id, $ order_type ){
  7. $ Array_temp = array ();
  8. Foreach ($ array_name as $ key => $ value) {// loop layer;
  9. $ Array_temp [$ key] = $ value [$ row_id]; // create a one-dimensional array. The index value is the index value of a two-dimensional array; the value is the value of the project to be compared in a two-dimensional array;
  10. }
  11. If ($ order_type = "ASC "){
  12. Asort ($ array_temp );
  13. } Else {
  14. Arsort ($ array_temp );
  15. }
  16. $ Result_array = array ();
  17. Foreach ($ array_temp as $ key => $ value) {// iterate over the filtered array;
  18. $ Result_array [] = $ array_name [$ key]; // create a new result array. change the original input key-value sequence and assign the value to the result array (the original array remains unchanged );
  19. }
  20. Return $ result_array;
  21. }
  22. // The following is the test
  23. $ Arr = array ('num' => 7, 'value' => 4 ),
  24. Array ('num' => 3, 'value' => 45 ),
  25. Array ('num' => 38, 'value' => 27 ));
  26. $ Sortarr = array_sort ($ arr, 'value', 'asc ');
  27. Print_r ($ sortarr );
  28. ?>

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.