PHP array sorting method summary recommended favorites _ PHP Tutorial

Source: Internet
Author: User
PHP array sorting method is recommended. With the rapid development of PHP, more and more people are using it. in the PHP array learning excerpt, I learned about the establishment of the most basic PHP array and the display of array elements. You need to thoroughly study the PHP array. With the rapid development of PHP, more and more people are using it. in the PHP array learning excerpt section, I learned about the establishment of the most basic PHP array and the display of array elements. You need to learn more about PHP array operations. The first issue is PHP array sorting and descending sorting.

Sort: This function assigns a new key name to the cell in array. This will delete the original key name, not just the reorder.
Rsort: This function sorts the array in reverse order (up to the lowest ). Deleting the original key name is not just a re-sorting.
Asort: sorts arrays and maintains the index relationship.
Arsort: sorts arrays in reverse order and maintains the index relationship.

Ksort: sorts the array by key name and retains the association between key names and data.
Krsort: sorts the array in reverse order by key name, and retains the association between key names and data.

Natsort: sorts alphanumeric strings and keeps the original key/value Association.
Natcasesort: same as natsort, but not case sensitive.


PHP array sorting (sort)
Sort numeric index arrays:
Function: sort (array, [sort type])
Note: the sort () function sorts the specified array (the first parameter) in ascending order.
The second parameter of the sort function is used to specify the sorting type. it is an optional parameter and the possible value is:
SORT_REGULAR: default value. sorting is performed without changing the type;
SORT_NUMERIC: sorts values as numbers;
SORT_STRING: sorts values as strings;
If the array contains 4 and "37", sort by number, 4 is less than "37"; sort by string, 4 is greater than "37 ″;

The code is as follows:


$ A = array (4, "37", 3,100, 0,-5 );
Sort ($ );
For ($ I = 0; $ I <6; ++ $ I ){
Echo $ a [$ I]. "";
}
Echo"
";
Sort ($ a, SORT_STRING );
For ($ I = 0; $ I <6; ++ $ I ){
Echo $ a [$ I]. "";
}
Echo"
";
?>


Output result:

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

Sort in descending order: rsort (array, [sort type])
The parameter usage is the same as that of the sort function.

Sort joined arrays:
Function: asort (array, [sort type])
Note: sort the element values of the associated array in ascending order. See the above sort function for parameters.

Function: ksort (array, [sort type])
Note: sort by the keywords of the associated array in ascending order. See the above sort function for parameters.


The code is as follows:


$ A = array (
"Good" => "bad ",
"Right" => "wrong ",
"Boy" => "girl ");

Echo "value sort
";
Asort ($ );
Foreach ($ a as $ key => $ value ){
Echo "$ key: $ value
";
}

Echo"
Key sort
";
Ksort ($ );
Foreach ($ a as $ key => $ value ){
Echo "$ key: $ value
";
}
?>


Output result:

Value sort
Good: bad
Boy: girl
Right: wrong

Key sort
Boy: girl
Good: bad
Right: wrong
Sort in descending order:
Arsort (array, [sort type]) corresponds to asort
Krsort (array, [sort type]) corresponds to ksort


Quickly create an array function range ()

For example, the range () function can quickly create a number array from 1 to 9:

The code is as follows:


$ Numbers = range (1, 9 );
Echo $ numbers [1];
?>


Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, range () can also create a character array from a to z:

The code is as follows:


$ Numbers = range (a, z );
Foreach ($ numbers as $ mychrs)
Echo $ mychrs ."";
?>



Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different. The range () function also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7. Common PHP array sorting: elements in an array are represented by characters or numbers. Therefore, you can sort array elements in ascending order. This function is sort (). For example:

The code is as follows:


$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
Sort ($ people );
Echo"
--- After sorting ---
";
Foreach ($ people as $ mychrs)
Echo $ mychrs ."";
?>


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

The Sort () function also has a second parameter, which indicates whether the PHP array sorting ascending rule is used to compare numbers or strings. For example:

The code is as follows:


Echo "--- sort by numbers in ascending order ---
";
$ Num2 = array ('26', '3 ',);
Sort ($ num2, SORT_NUMERIC );
Foreach ($ num2 as $ mychrs)
Echo $ mychrs ."";
Echo"
--- Sort by character in ascending order ---
";
$ Num3 = array ('26', '3 ');
Sort ($ num3, SORT_STRING );
Foreach ($ num3 as $ mychrs)
Echo $ mychrs ."";
?>


SORT_NUMERIC and SORT_STRING are used to declare the ascending order of numbers or characters. If the numbers are listed in ascending order: 3, 26, but if the characters are listed in ascending order, they are listed as 26, 3. In PHP, in addition to the ascending function, there are also descending or inverse sort functions, which are rsort () functions, such as: $ num1 = range (); rsort ($ num1 ); this is actually equivalent to range (9, 1 ).

Bytes. Need to learn more about the PHP array...

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.