To sort a two-dimensional array by specifying items

Source: Internet
Author: User

  1. /*
  2. $array _name: the incoming array;
  3. $row _id: An array of items to sort;
  4. $order _type: Sort by the way, 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) {//cycle one layer;
  9. $array _temp[$key]= $value [$row _id];//Create a new one-dimensional array, the index value is the index value of the two-dimensional array, and the value is the value of the item to be compared by 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) {//pairs filtered array traversal;
  18. $result _array[]= $array _name[$key];//Create a new result array and assign the original passed array to the result array after changing the order of the key values (the original array is unchanged);
  19. }
  20. return $result _array;
  21. }
  22. Here is the test
  23. $arr =array (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. ?>
Copy Code
  • 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.