Summary of PHP array sorting methods

Source: Internet
Author: User
Tags sorts

Sort: This function assigns a new key name to the cells in the array. This will delete the original key name and not just reorder it.
Rsort: This function reverses the order of the array (highest to lowest). Delete the original key name and not just reorder it.
Asort: Sorting an array and maintaining an index relationship
Arsort: Reverse-sort an array and maintain an index relationship

Ksort: The array is sorted by key name, preserving the association of the key name to the data
Krsort: The array is reversed by the key name, preserving the association of the key name to the data

Natsort: Sorting alphanumeric strings and preserving the association of the original key/value
Natcasesort: Sort algorithm with Natsort, but not case-sensitive alphabetical sort


PHP array sorting (sort)
Sorting Numeric index arrays:
Functions: Sort (array, [Sort type])
Description: the sort () function sorts the specified array (the first parameter) in ascending order.
The second parameter of the sort function is the specified sort type, which is an optional parameter and the possible values are:
Sort_regular: Default value, do not change type to sort;
Sort_numeric: Sort the values as numbers;
Sort_string: Sorts the values as strings;
An array of 4 and "37″, sorted by number, 4 less than" 37″; sorted by string, 4 greater than "37″;

Copy CodeThe code is as follows:
<?php
$a = Array (4, "37", 3,100,0,-5);
Sort ($a);
for ($i =0; $i <6; + + $i) {
echo $a [$i]. " ";
}
echo "<br/>";
Sort ($a, sort_string);
for ($i =0; $i <6; + + $i) {
echo $a [$i]. " ";
}
echo "<br/>";
?>


Output Result:

-5 0 3 4 37 100
-5 0 100 3 37 4

Sort Descending: Rsort (array, [Sort type])
The parameter usage is the same as the sort function.

associative array Ordering:
Function: Asort (array, [Sort type])
Description: Sorts ascending order based on the element values of the associative array. The parameters are used in the sort function above.

Function: Ksort (array, [Sort type])
Description: Sort Ascending According to the keyword of the associative array. The parameters are used in the sort function above.


Copy CodeThe code is as follows:
<?php
$a = Array (
"Good" = "bad",
"Right" = "wrong",
"Boy" = "girl");

echo "Value sort<br/>";
Asort ($a);
foreach ($a as $key = = $value) {
echo "$key: $value <br/>";
}

echo "<br/>key sort<br/>";
Ksort ($a);
foreach ($a as $key = = $value) {
echo "$key: $value <br/>";
}
?>


Output Result:

Value sort
Good:bad
Boy:girl
Right:wrong

Key sort
Boy:girl
Good:bad
Right:wrong
Sort Descending:
Arsort (array, [sort type]) corresponds to Asort
Krsort (array, [sort type]) corresponds to Ksort


Quickly create an array of functions range ()

For example, the range () function can quickly create arrays of numbers from 1 to 9:

Copy CodeThe code is as follows:
<?php
$numbers =range (1,9);
echo $numbers [1];
?>


Of course, using range (9,1) creates an array of numbers from 9 to 1. Also, range () can create an array of characters from A to Z:

Copy CodeThe code is as follows:
<?php
$numbers =range (a,z);
foreach ($numbers as $mychrs)
echo $mychrs. " ";
?>



Note the case when using a character array, such as range (A,Z) and range (A,Z) are not the same. The range () function also has a third argument that sets the step size, such as the array elements created by range (1,9,3): 1, 4, 7. Common PHP array sorting the elements in a general array are represented by characters or numbers, so you can sort the elements in ascending order, the function is sort (). Like what:

Copy CodeThe code is as follows:
<?php
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
foreach ($people as $mychrs)
echo $mychrs. " ";
Sort ($people);
echo "<br/>---after sorting---<br/>";
foreach ($people as $mychrs)
echo $mychrs. " ";
?>


An ascending sorted array element is displayed as birth name Nation sex, of course, the sort () function is case-sensitive (Letters from the largest to smallest order are: A ... Z...A...Z)

The sort () function also has a second parameter, which is used to indicate whether the rule in ascending order of the PHP array is to compare numbers or strings. Like what:

Copy CodeThe code is as follows:
<?php
echo "---sort---<br/> in ascending order of numbers";
$num 2=array (' 26 ', ' 3 ',);
Sort ($num 2,sort_numeric);
foreach ($num 2 as $mychrs)
echo $mychrs. " ";
echo "<br/>---sorted by word Fu Shen order---<br/>";
$num 3=array (' 26 ', ' 3 ');
Sort ($num 3,sort_string);
foreach ($num 3 as $mychrs)
echo $mychrs. " ";
?>


Sort_numeric and sort_string are used to declare in ascending order of numbers or characters. If the numbers are sorted in ascending order: 3, 26, but if ordered by word Fu Shen: 26, 3. In addition to the ascending function in PHP, there are functions in descending or reverse order, that is, the Rsort () function, such as: $num 1=range (1,9), Rsort ($num 1), which is actually equivalent to range (9,1).

Summary of PHP array sorting methods

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.