PHP two-dimensional array sorting-PHP source code

Source: Internet
Author: User
PHP two-dimensional array sorting php code

/*** @ Package 2D array sorting * @ version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $ *** Sort an two-dimension array by some level two items use array_multisort () function. ** sysSortArray ($ Array, "Key1", "SORT_ASC", "SORT_RETULAR", "Key2 "......) * @ Author Chunsheng Wang
 
  
* @ Param array $ ArrayData the array to sort. * @ param string $ KeyName1 the first item to sort. * @ param string $ SortOrder1 the order to sort by ("SORT_ASC" | "SORT_DESC ") * @ param string $ SortType1 the sort type ("SORT_REGULAR" | "SORT_NUMERIC" | "SORT_STRING") * @ return array sorted array. */function sysSortArray ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR") {if (! Is_array ($ ArrayData) {return $ ArrayData;} // Get args number. $ ArgCount = func_num_args (); // Get keys to sort by and put them to SortRule array. for ($ I = 1; $ I <$ ArgCount; $ I ++) {$ Arg = func_get_arg ($ I); if (! Eregi ("SORT", $ Arg) {$ KeyNameList [] = $ Arg; $ SortRule [] = '$ '. $ Arg;} else {$ SortRule [] = $ Arg ;}// Get the values according to the keys and put them to array. foreach ($ ArrayData AS $ Key => $ Info) {foreach ($ KeyNameList AS $ KeyName) {$ KeyName} [$ Key] = $ Info [$ KeyName] ;}// Create the eval string and eval it. $ EvalString = 'Array _ multisort ('. join (",", $ SortRule ). ', $ ArrayData);'; eval ($ EvalString); return $ ArrayData ;}
 

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.