Php array sorting methods

Source: Internet
Author: User
Tags arrays mixed php and sorts

The most common function of array sorting is sort ($ arr). It is used to sort the key values of an array in ascending order, and the sorted array key name is no longer the original key name, is the key name reset by the new array.
Sometimes we need more complex sorting. For example, to sort key names, ksort ($ arr) is used here. It sorts the keys of the array and maintains the original key-value relationship. The corresponding asort ($ arr) function is used to sort key values and maintain the original key-value relationship.

In the same principle, rsort (); arsort (); krsort (); functions are sorted in descending order, and other functions include sort (); rsort (); ksort (); same.
Array operations are an important foundation for PHP and we hope to make good use of them.


Sort key values
That is, the sequence is arranged by the size of the ASC Ⅱ value.

Ksort (): Sort by array identifier
Krsort (): Sort by array identifier in reverse order
Instance 1:

The code is as follows: Copy code

<? Php
$ Ages = array (
'C' => 'php ',
'D' => 'asp ',
'A' => 'JSP ',
'B' => 'Java'
);
Krsort ($ ages );
Foreach ($ ages as $ key => $ val ){
Echo "$ key = $ val". '<br> ';
};
?>

Sort by element value

Asort (): sorts arrays in ascending order;
Rsort (): sort the array in descending order.
 
Change the line 8-11 of instance 1:

The code is as follows: Copy code

Asort ($ ages );
Print_r ($ ages );
Echo "<br> ";
Rsort ($ ages );
Print_r ($ ages );


Delete the sorting of original key names

Sort (): sorts arrays in ascending order;
Rsort (): sort the array in descending order.

Change the 8-11 line code of instance 2:

The code is as follows: Copy code

Sort ($ ages );
Foreach ($ ages as $ key => $ val ){
Echo "ages [$ key] = $ val". '<br> ';
};

Array_multisort -- sorts multiple arrays or multi-dimensional arrays.
Description
Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])

Example 1. Sort multiple arrays

The code is as follows: Copy code

<? Php
$ Ar1 = array ("ten", 100,100, "");
$ Ar2 = array (1, 3, "2", 1 );
Array_multisort ($ ar1, $ ar2 );

Var_dump ($ ar1 );
Var_dump ($ ar2 );
?>

In this example, after sorting, the first array will contain "10", "a", 100,100. The second array will contain 1, 1, "2", 3. The project order in the second array is exactly the same as that in the first array (100 and 100.

The code is as follows: Copy code

Array (4 ){
[0] => string (2) "10"
[1] => string (1) ""
[2] => int (100)
[3] => int (100)
}
Array (4 ){
[0] => int (1)
[1] => int (1)
[2] => string (1) "2"
[3] => int (3)
}

 

Example 2. Sort multidimensional arrays

The code is as follows: Copy code

<? Php
$ Ar = array ("10", 100,100, "a"), array (1, 3, "2", 1 ));
Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );
?>

 

In this example, after sorting, the first array will contain 10,100,100, "a" (as the string ascending order), and the second array will contain 1, 3, "2 ", 1 (sort the values in descending order ).


PHP array sorting functions are many and powerful.
For example: arsort, asort, krsort, ksort, sort...
But sometimes it cannot meet our needs.
If there is a two-dimensional array, we need to sort it by a value in the two-dimensional array.
You need to use usort to sort custom arrays.


Example:

The code is as follows: Copy code

 
<? Php
 
$ Aa = array ("score" => 60 ),
Array ("score" => 70 ),
Array ("score" => 80 ),
Array ("score" => 90 ),
Array ("score" => 20 ),
Array ("score" => 10 ),
Array ("score" => 50 ),
Array ("score" => 30 ));
 
Echo '------ output before sorting ------ <br/> ';
Var_dump ($ aa); // output before sorting
 
Usort ($ aa, "cmp"); // sort (from large to small)
 
Echo '<br/> ------ output after sorting ------ <br/> ';
Var_dump ($ aa); // sorted output
 
/**
* Custom sorting conditions
* @ Param array $
* @ Param array $ B
* @ Return bool
*/
Function cmp ($ a, $ B ){
If ($ a ["score"] = $ B ["score"]) {
Return 0;
 }
Return ($ a ["score"] <$ B ["score"])? 1:-1;
}
 
?>

Output result:

The code is as follows: Copy code

------ Output before sorting ------
Array (8) {[0] => array (1) {["score"] => int (60 )}
[1] => array (1) {["score"] => int (70 )}
[2] => array (1) {["score"] => int (80 )}
[3] => array (1) {["score"] => int (90 )}
[4] => array (1) {["score"] => int (20 )}
[5] => array (1) {["score"] => int (10 )}
[6] => array (1) {["score"] => int (50 )}
[7] => array (1) {["score"] => int (30 )}}
------ Output after sorting ------
Array (8) {[0] => array (1) {["score"] => int (90 )}
[1] => array (1) {["score"] => int (80 )}
[2] => array (1) {["score"] => int (70 )}
[3] => array (1) {["score"] => int (60 )}
[4] => array (1) {["score"] => int (50 )}
[5] => array (1) {["score"] => int (30 )}
[6] => array (1) {["score"] => int (20 )}
[7] => array (1) {["score"] => int (10 )}}

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.