In php, array sorting has always been a common issue. Next we will focus on the Implementation Program of the one-dimensional array and two-dimensional array sorting in php. You can refer to it.
Function: sorts arrays again.
(Bubble sort (one-dimensional array) (a two-dimensional array of a healthy sorting)
Compare the size of the elements to be sorted. If the two data elements are in the opposite order, they are exchanged until there is no reverse order of the data elements.
Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, scanning the array from bottom up, where a light bubble that violates the principle is scanned, it will "float" up ". this is repeated. until the last two of the above are light, the severe ones.
The Code is as follows: |
Copy code |
/** * Bubble sort (one-dimensional array) * Compare the size of the elements to be sorted. If the two data elements are in the opposite order, they are exchanged until there is no data element in the reverse order. * Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, scanning the array from bottom up, where a light bubble that violates the principle is scanned, it will "float" up ". this is repeated. * Until the last two parties are put on the lighter ones, the heavier ones will be placed on the lighter ones. */ Function bubble_sort ($ array, $ key = null ){ $ Count = count ($ array ); If ($ count <0 ){ Return false; } For ($ I = 0; $ I <$ count; $ I ++ ){ For ($ j = $ count-1; $ j> $ I; $ j --){ If ($ key & isset ($ array [$ key]) {// two-dimensional array Jian existence If ($ array [$ j] [$ key] <$ array [$ j-1] [$ key]) { $ Tmp = $ array [$ j]; $ Array [$ j] = $ array [$ j-1]; $ Array [$ j-1] = $ tmp; } } Else {// One-dimensional array If ($ array [$ j] <$ array [$ j-1]) { $ Tmp = $ array [$ j]; $ Array [$ j] = $ array [$ j-1]; $ Array [$ j-1] = $ tmp; } } } } Return $ array; } |
How to sort array_multisort
How to Use array_multisort () to sort two arrays by specified key values
In this example, a two-dimensional array named $ data is defined and sorted as follows:
The Code is as follows: |
Copy code |
$ Data [] = array ('Volume '=> 67, 'version' => 2 ); $ Data [] = array ('Volume '=> 86, 'version' => 1 ); $ Data [] = array ('Volume '=> 85, 'version' => 6 ); $ Data [] = array ('Volume '=> 98, 'version' => 2 ); $ Data [] = array ('Volume '=> 86, 'version' => 6 ); $ Data [] = array ('Volume '=> 67, 'version' => 7 );
// Retrieve the column list Foreach ($ data as $ key => $ row ){ $ Volume [$ key] = $ row ['Volume ']; $ Edition [$ key] = $ row ['version']; }
// Sort data by volume in descending order and edition in ascending order // Use $ data as the last parameter and sort it by a common key Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
Print_r ($ data ); The output is as follows:
Array ( [0] => Array ( [Volume] => 98 [Edition] => 2 ) [1] => Array ( [Volume] => 86 [Edition] => 1 ) [2] => Array ( [Volume] => 86 [Edition] => 6
[3] => Array ( [Volume] => 85 [Edition] => 6 ) [4] => Array ( [Volume] => 67 [Edition] => 2 ) [5] => Array ( [Volume] => 67 [Edition] => 7 ) ) |
We only need to use sort () to sort one-dimensional arrays. The corresponding asort ($ arr) function is 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.