PHP Custom two-dimensional array sorting function array

Source: Internet
Author: User
Tags array sort
This time, we'll bring you a PHP custom two-dimensional array sort function array,php custom two-dimensional array sorting function what are the considerations for array, here is the actual case, take a look.

This article mainly introduces the PHP custom two-dimensional array sorting function Array_orderby usage, combined with the example of PHP for two-dimensional array to sort the related traversal, determination, sequencing and other operational skills, the need for friends can refer to the next

This article describes the PHP custom two-dimensional array ordering function Array_orderby usage. Share to everyone for your reference, as follows:

<?php/**i came up with a easy to sort database-style results. This does-example 3 does, except it takes care of creating those intermediate arrays for you before passing control O N to Array_multisort ().  */function Array_orderby () {$args = Func_get_args ();  $data = Array_shift ($args);      foreach ($args as $n = + $field) {if (is_string ($field)) {$tmp = array ();      foreach ($data as $key = + $row) $tmp [$key] = $row [$field];      $args [$n] = $tmp;  }} $args [] = & $data;  Call_user_func_array (' Array_multisort ', $args); Return Array_pop ($args);} /*the sorted array is now with the return value of the function instead of being passed by reference.*/$data [] = Array (' Volu Me ' = ' Edition ' + 2); $data [] = Array (' volume ' = 85, ' edition ' = 1); $data [] = Array (' volume ' =, ' Edition ' + 6); $data [] = Array (' volume ' = = 98, ' edition ' = + 2); $data [] = Array (' volume ' = N, ' edition ' =&gt ; 6); $data [] = Array (' volume ' = +, ' EdItion ' + 7);//Pass the array, followed by the column names and sort flags$sorted = Array_orderby ($data, ' volume ', SOR T_desc, ' edition ', SORT_ASC);p rint_r ($sorted)?>

Operation Result:

Array (  [0] = = Array    (      [Volume] = 98      [Edition] = 2    )  [1] = = Array    (      [ Volume] [      Edition] = 1    )  [2] = = Array    (      [Volume] = [      Edition] = 6< c14/>)  [3] = = Array    (      [Volume] =      [Edition] = 6    )  [4] = = Array    (      [Volume] =      [Edition] + 2    )  [5] = =    Array      ([volume] = [      Edition] = 7    ))

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How the PHP generator uses

phpstudy2018 Access to directory service permissions

thinkphp Implementation Payment (JSAPI payment) process Tutorial _php instance

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.