Php array sorting methods summary _ PHP Tutorial

Source: Internet
Author: User
Summary of various methods for sorting php arrays. In php, we need to sort one-dimensional arrays. we only need to use sort () and rsort, if you want to sort multi-dimensional data, php does not have such a function. in php, we need to sort one-dimensional arrays very easily. we only need to use sort (), rsort () this is done. if you want to sort multi-dimensional data, we need to do this by ourselves before such functions are available in php.

ForPhp array sortingMany functions are provided in php. I will list them below, which can be supplemented by incomplete functions.

The sort () function is used to sort array units from low to high.
The rsort () function is used to sort array units from high to low.
The asort () function is used to sort array units from low to high and maintain the index relationship.
Arsort () is used to sort array units from high to low and maintain the index relationship.
The ksort () function is used to sort array units by key names from low to high.
The krsort () function is used to sort array units by key names from high to low.

But today we are mainly talking about php's built-in array sorting function, mainly about custom sorting.


I. Bubble sorting
Note: find the maximum number, arrange it to the last side, and continue to find it.

Example:

The code is as follows:
$ Arr = array (3,5,-1,0, 2 );
For ($ I = 0; $ I For ($ j = 0; $ j If ($ arr [$ j]> $ arr [$ j + 1]) {
$ Temp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ temp;
}
}
}

Understanding:
3, 5,-1, 0, 2
// Compare the values starting from the first number.
// For the first time, 3 is less than 5, so it remains unchanged
// For the second time, if 5 is greater than-1
3 ,-,
// The third time, 5 is greater than 0
3,-1, 0, 5, 2
// The fourth time, 5 is greater than 2
3,-1, 0, 2, 5
So far, an internal loop is completed, and the last number is sorted.
3,-, the second external loop starts the first time: 3 is greater than-1
-1, 3, 0, 2, 5
Second: 3 is greater than 0
-1, 0, 3, 2, 5
Third time: 3 is greater than 2
-1, 0, 2, 3, 5
So far, the sorting of the next two digits is completed, and so on.
-1, 0, 2, 3, 5
II. sorting method: assume that the first number is the smallest number, and then compare the subsequent number with it in sequence. if the number is not the smallest, change it to the smallest number.

The code is as follows:
$ Arr = array (2, 1,-1, 3, 0 );
For ($ I = 0; $ I $ Minval = $ arr [$ I];
$ Minindex = $ I;
For ($ j = 1 + $ I; $ j If ($ arr [$ j] <$ minval ){
$ Minval = $ arr [$ j];
$ Minindex = $ j;
}
}
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ minindex];
$ Arr [$ minindex] = $ temp;
}

Understanding:
2, 1,-1, 3, 0
// Assume that the first number 2 is the minimum value, and the number after it is compared with 2 in sequence to find the minimum number
Process:
1 is less than 2, so minval = 1
-1 is less than 1, then minval =-1
3 is greater than-1, unchanged
0 is greater than-1, unchanged
Now, the minimum number in the array is-1.
Change the positions of-1 and 2 to sort the first number.
Now the array is changed
-1, 1, 2, 3, 0
Now the first number-1 is in order, so we do not participate in the comparison.
Now suppose minval = 1
2 is greater than 1, unchanged
3 is greater than 1, unchanged
If 0 is less than 1, minval = 0
Now a loop is completed, and the second number is sorted by changing the positions 0 and 1.
Now the array is changed
-1, 0, 2, 3, 1
// The following method is the same as above...

III. Insert sorting method description: assume that the first number in an array is a separate ordered array, and then the next number and it [here with its I increase, it turns into them.] if the number behind is smaller than the assumed number, move the smaller number to the beginning.

The code is as follows:
$ Arr = array (2, 1,-1, 3, 0 );
For ($ I = 1; $ I $ Insertval = $ arr [$ I];
$ Insertindex = $ I-1;
While ($ insertindex> = 0 & $ insertval <$ arr [$ insertindex]) {
$ Arr [$ insertindex + 1] = $ arr [$ insertindex];
$ Insertindex --;
}
$ Temp = $ arr [$ I];
$ Arr [$ insertindex + 1] = $ insertval;
}

Understanding:
2, 1,-1, 3, 0
// For the first time, first save the number 1 to be inserted as insertval, and then compare insertval with 2. 1 is smaller than 2, so move 2 back to the following figure.
2, 2,-1, 3, 0
// At this time, there is no number before 2, insertindex = 0, so the comparison is complete, then insert insertval to the position found. Change to as shown in figure
1, 2,-1, 3, 0
// At this time, 1 or 2 is an ordered array
// For the second time, first save the number to be inserted-1 as insertval, then compare insertval with 2,-1 is smaller than 2, so move 2 back, as shown in
1, 2, 3, 0
// At this time, compare insertval with 1. if-1 is smaller than 1, move-1 back and change it to an example (this is a process of comparing the number to be inserted with the preceding ordered array)
1, 1, 2, 3, 0
// At this time, insertindex is in the beginning, so insert insertval into this position
-1, 1, 2, 3, 0
// The Push method is as follows:


The two-dimensional array sorting function can achieve the order by effect similar to MySQL. when the array is not obtained from the database, there will be special applications.

The code is as follows:

// Description: sorting of two-dimensional arrays in PHP


/**
* @ 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
* @ 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 );

?>

As for the sorting of one-dimensional arrays, we can use the built-in functions of php to fully implement data sorting. Therefore, we are talking about practices that cannot meet our needs with custom functions.

Sort () and rsort () are completed in this way. if you want to sort multi-dimensional data, php does not have such a function. This requires me...

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.