In php, we can directly use the array_multisort () function to sort two-dimensional arrays. At the same time, I have also added some custom 2D array sorting codes for your reference. In php, we can directly use the array_multisort () function to sort two-dimensional arrays. At the same time, I have also added some custom 2D array sorting codes for your reference.
Script ec (2); script
First, let's look at the array_multisort () function.
| The Code is as follows: |
|
|
$ Ar = array (
Array ("10", 11,100,100, ""),
Array (1, 2, "2", 3, 1)
);
Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );
Var_dump ($ ar );
?> |
In this example, after sorting, the first array will become "10", 100,100, 11, "a" (sorted in ascending order as strings ). The second array will contain 1, 3, "2", 2, 1 (arranged in descending order as numbers ).
| The Code is as follows: |
|
|
Array (2 ){
[0] => array (5 ){
[0] => string (2) "10"
[1] => int (100)
[2] => int (100)
[3] => int (11)
[4] => string (1) ""
}
[1] => array (5 ){
[0] => int (1)
[1] => int (3)
[2] => string (1) "2"
[3] => int (2)
[4] => int (1)
}
} |
It is not very convenient to use existing functions. I will recommend a user-defined function.
| The Code is as follows: |
|
Function array_sort ($ arr, $ keys, $ type = 'asc '){
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k =>$ v ){
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc '){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
} |
Test (it can sort two-dimensional arrays by specified key values, or specify the ascending or descending sort method (the default is ascending ),
Usage example:
| The Code is as follows: |
|
|
$ Array = array (
Array ('name' => 'phone', 'brand' => 'nokia ', 'price' => 1050 ),
Array ('name' => 'laptop ', 'brand' => 'lenovo', 'price' => 4300 ),
Array ('name' => 'shares', 'brand' => 'phillips', 'price' => 3100 ),
Array ('name' => 'treadmill ', 'brand' => 'three', 'price' => 4900 ),
Array ('name' => 'Watch ', 'brand' => 'case', 'price' => 960 ),
Array ('name' => 'LCD TV', 'brand' => 'sony ', 'price' => 6299 ),
Array ('name' => 'laser printer ', 'brand' => 'hp', 'price' => 1200)
);
$ ShoppingList = array_sort ($ array, 'price ');
Print_r ($ ShoppingList ); |
Bubble Method
| The Code is as follows: |
|
|
Function arraysort ($ data, $ order = 'asc '){
// Asc ascending desc descending
$ Temp = array ();
$ Count = count ($ data );
If ($ count <= 0)
Return false; // the input data is incorrect.
If ($ order = 'asc '){
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ data [$ j] <$ data [$ j-1]) {
// Exchange two data locations
$ Temp = $ data [$ j];
$ Data [$ j] = $ data [$ j-1];
$ Data [$ j-1] = $ temp;
}
}
}
} Else {
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ data [$ j]> $ data [$ j-1]) {
$ Temp = $ data [$ j];
$ Data [$ j] = $ data [$ j-1];
$ Data [$ j-1] = $ temp;
}
}
}
}
Return $ data;
}
$ Data = array (7, 5, 3, 8, 9, 1, 5, 3, 1, 24, 3, 87, 0, 33, 1, 12, 34, 54, 66, 32 );
Var_dump (arraysort ($ data); // ascending
Echo ('
');
Var_dump (arraysort ($ data, 'desc'); // descending order |