Php two-dimensional array sorting and default natural sorting methods

Source: Internet
Author: User

Php two-dimensional array sorting function, which is a natural sorting by default, that is, sort sorting. You can specify multiple methods to sort values by values in a two-dimensional array. For more information, see the program annotations below.
Copy codeThe Code is as follows:
/**

* @ Function two-dimensional array natural sorting

* @ Author www.phpernote.com

* @ Param array $ array the array to be sorted (two-dimensional)

* @ Param string key to be sorted by which key

* @ Param string order sorting method (SORT_ASC, SORT_DESC)

* @ Param string type sorting method (SORT_REGULAR, SORT_NUMERIC, SORT_STRING)

* @ Return array sorted array.

*/

Function phpSortArray ($ array, $ key, $ order = "SORT_ASC", $ type = "SORT_REGULAR "){

If (! Is_array ($ array) | empty ($ array )){

Return $ array;

}

$ ArgCount = func_num_args (); // returns the number of parameters passed to the currently defined function.

For ($ I = 1; $ I <$ ArgCount; $ I ++ ){

$ Arg = func_get_arg ($ I );

If (! Eregi ("SORT", $ Arg )){

$ KeyNameList [] = $ Arg;

$ SortRule [] = '$'. $ Arg;

} Else {

$ SortRule [] = $ Arg;

}

}

Foreach ($ array AS $ Key => $ Info ){

Foreach ($ KeyNameList as $ KeyName ){

$ {$ KeyName} [$ Key] = $ Info [$ KeyName];

}

}

$ EvalString = 'array _ multisort ('. join (",", $ SortRule).', $ array );';

Eval ($ EvalString );

Return $ array;

}

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.