PHP Array Sorting method summary recommended collection _php tips

Source: Internet
Author: User
Tags array sort sorts

With the rapid development of PHP, with its people more and more, in the PHP array learning excerpt section to learn the most basic PHP array of the establishment and the number of elements of the display. Need to learn more about the operation of the PHP array. The first contact is the PHP array sort, descending sort problem.

Sort: This function assigns a new key name to the cell in the array. This deletes the original key name and not just the reordering.
Rsort: This function is reversed (highest to lowest) in the array. Deletes the original key name and not just the reordering.
Asort: Sorting arrays and keeping index relationships
Arsort: Reverse-sort the array and keep the index relationship

Ksort: Arrays are sorted by key name, preserving association of key names to data
Krsort: Arrays are sorted in reverse by key names, preserving the association of key names to data

Natsort: Sort alphanumeric strings and keep the existing key/value associations
Natcasesort: The same natsort sort algorithm, but not case-sensitive alphabetical sorting


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

Copy Code code 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 results:

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

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

associative array Ordering:
Functions: Asort (array, [Sort type])
Description: Sorts the ascending sort according to the element values of the associative array. parameter is used to see the sort function above.

Functions: Ksort (array, [Sort type])
Description: Sorts the ascending sort according to the keywords of the associative array. parameter is used to see the sort function above.


Copy Code code 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 results:

Value sort
Good:bad
Boy:girl
Right:wrong

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


quickly create an array of functions range ()

For example, the range () function can quickly create an array of numbers from 1 to 9:
Copy Code code as follows:

<?php
$numbers =range (1,9);
echo $numbers [1];
?>

Of course, using range (9,1) creates a 9 to 1 array of numbers. At the same time, range () can also create an array of characters from A to Z:
Copy Code code as follows:

<?php
$numbers =range (a,z);
foreach ($numbers as $mychrs)
echo $mychrs. " ";
?>


Note case when using a character array, such as range (A,Z) and range (A,Z) are different. The range () function also has a third parameter, which is to set the step length, such as the array elements created by range (1,9,3) are: 1, 4, 7. Common PHP Array sort the elements in a generic array are represented by characters or numbers, so you can arrange the arrays in ascending order, the function is sort (). Like what:
Copy Code code as follows:

<?php
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
foreach ($people as $mychrs)
echo $mychrs. " ";
Sort ($people);
echo "<br/>---sorted---<br/>";
foreach ($people as $mychrs)
echo $mychrs. " ";
?>

The array elements that are sorted in ascending order are displayed as birth name Nation sex, and of course the sort () function is case-sensitive (letters from large to small) in the order that a ... Z...A...Z)

The sort () function also has a second parameter, which is used to indicate whether the rules of the sort ascending of the PHP array are used to compare numbers or strings. Like what:
Copy Code code as follows:

<?php
echo "---sorted by number in ascending order---<br/>";
$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 ascending order by number or character. If you are in ascending order of numbers: 3, 26, but if you follow the word Fu Shen order is: 26, 3. In addition to the ascending function in PHP, there is a descending or inverted function, which is the Rsort () function, such as: $num 1=range (1,9), Rsort ($num 1), which is actually equivalent to range (9,1).

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.