Steps for sorting two-dimensional arrays in PHP-php Tutorial

Source: Internet
Author: User
Sequencing of two-dimensional arrays in PHP & lt ;? Php *** @ package ???? BugFree * @ version ???? $ Id: FunctionsMain. inc. php, v1.322005092411: 38: 37 wwccssExp $ *** Sortantwo-dimensionarraybysomelevelt PHP two-dimensional array sorting method

/**
* @ Package ???? BugFree
* @ 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 <[email protected]>
* @ 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;
}
?
?
#################
$ Arr = array (
??? Array (
??????? 'Name '??????? ==> ??? 'Learn ',
??????? 'Size '??????? ==> ??? '123 ',
??????? 'Type '??????? ==> ??? 'Jpe ',
??????? 'Time '??????? ==> ??? '2017-11-13 ',
??????? 'Class '??????? ==> ??? 'DD ',
??? ),
??? Array (
??????? 'Name '??????? ==> ??? 'Kung Fu China ',
??????? 'Size '??????? ==> ??? '123 ',
??????? 'Type '??????? ==> ??? 'Jpe ',
??????? 'Time '??????? ==> ??? '2017-11-13 ',
??????? 'Class '??????? ==> ??? 'JJ ',
??? ),
??? Array (
??????? 'Name '??????? ==> ??? 'Programmatically ',
??????? 'Size '??????? ==> ??? '35 ',
??????? 'Type '??????? ==> ??? 'Gif ',
??????? 'Time '??????? ==> ??? '2017-11-13 ',
??????? 'Class '??????? ==> ??? 'DD ',
??? ),
??? Array (
??????? 'Name '??????? ==> ??? 'Kung Fu China ',
??????? 'Size '??????? ==> ??? '65 ',
??????? 'Type '??????? ==> ??? 'Jpe ',
??????? 'Time '??????? ==> ??? '2017-02-13 ',
??????? 'Class '??????? ==> ??? 'Yy ',
??? ),
??? Array (
??????? 'Name '??????? ==> ??? 'Kung Fu China ',
??????? 'Size '??????? ==> ??? '5 ',
??????? 'Type '??????? ==> ??? 'Icon ',
??????? 'Time '??????? ==> ??? '2017-12-13 ',
??????? 'Class '??????? ==> ??? 'Rr ',
??? ),
);
?
Print_r ($ arr );
?
// Note: When sorting by number, 153 is smaller than 65.
$ Temp = sysSortArray ($ arr, "name", "SORT_ASC", "type", "SORT_DESC", "size", "SORT_ASC", "SORT_STRING ");
?
Print_r ($ temp );
?
?>

?

Other related sorting functions:

?

Sort (array & $ array [, int $ sort_flags])

This function sorts the target array, and each element is sorted in a descending order of values. note that this function parameter is passed by reference and is not returned as an array after sorting. instead, it only sorts the current array and does not return any value regardless of the result.
Note: This function assigns a new key name to the unit in array. This will delete the original key name, not just the reorder.

$arr = array(9,5,2,6,8,4,3,1); sort($arr); print_r($arr); ?>

Output: (we can see that the key-value association is no longer maintained)

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 8 [7] => 9 )

Asort (array & $ array [, int $ sort_flags])

This function sorts the array. The index of the array is associated with the unit. it is mainly used to sort the arrays that are very important to the unit order .? It is also passed by reference, with no return value.

 

Output (the key value remains unchanged ):

Array ( [5] => 1 [1] => 2 [4] => 4 [0] => 5 [2] => 6 [3] => 8 )

Rsort (array & $ array [, int $ sort_flags])

This function is similar to the sort function to reverse sort the array (maximum to lowest ).

Note :?This function is?Array? The unit in is assigned a new key name. This will delete the original key name, not just the reorder.

If yes, return?TRUE?FALSE.

Arsort (array & $ array [, int $ sort_flags])

This function sorts the array in reverse order, and the index of the array remains associated with the unit. It is mainly used to sort the arrays that are very important to the unit order. The arsort () function is the same as asort (), but it sorts array elements in reverse order (in descending order.

If yes, return?TRUE?FALSE.

Ksort (array & $ array [, int $ sort_flags])

The difference between ksort and sort is that the array is sorted by key name rather than by value, and the association between key names and data is retained. this function is mainly used to associate arrays. if the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Krsort (array & $ array [, int $ sort_flags])
See ksort. this function sorts the results of ksort in reverse order.

Natsort (array & $ array)

This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the Association of original keys/values. this is called "natural sorting ". the difference between this algorithm and the general computer string sorting algorithm (used for sort () is shown in the following example.

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.
There is a classic example in the php Manual.

 

The above example will output: Standard sorting

Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png )

Natural order sorting

Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )

Some array sorting functions useu(User) is started with some user-defined sorting functions. This is not due to the time.

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.