Ec (2); in the php tutorial, there are three sorting methods: Index sorting and value sorting (the original index is not retained) sort by values (Retain the original index ). Each function is divided into three types: ascending, descending, and user-defined order. They are as follows: sort by index: ① ascending ksort () ② descending krsort () ③ user-defined order uksort () without retaining the original index value for sorting: ① ascending sort () ② descending rsort () ③ user-defined order usort () retains the original "script" ec (2); "script"
Sorting of Arrays
In the php tutorial, there are three sorting methods: Index sorting, value sorting (without retaining the original index), and value sorting (Retain the original index ). Each function is divided into three types: ascending, descending, and user-defined order. They are as follows:
Sort by index: ① ascending ksort () ② descending krsort () ③ User-Defined ordered uksort ()
Sorting without retaining original index values: ① ascending sort () ② descending rsort () ③ user-defined order usort ()
Sort the original index values: ① asort () ② arsort () ③ user-defined order uasort ()
In php, you can also use array_multisort to sort multiple arrays at a time, but the project may be used less.
Flip the array, flip the numeric index, and the index starts from 0 again: array_reverse ()
Change the index and value: array_flip ()
Random order: shuffle ()
Iterator Traversal
$ Test01 = array ('A', 'B', 'C ');
//
For ($ I = 0; $ I <count ($ test01); $ I ++ ){
Echo $ test01 [$ I];
}
// Foreach value only
Foreach ($ test01 as $ value ){
Echo $ value;
}
// Foreach key and value
$ Test01 = array ('A' => 'aaa', 'B' => 'bbbbb', 'c' => 'cccc ');
Foreach ($ test01 as $ key => $ value ){
Echo "$ key => $ value ";
}
$ Test01 = array ('A' => 'aaa', 'B' => 'bbbbb', 'c' => 'cccc ');
While (list ($ key, $ value) = each ($ test01 )){
Echo "$ key => $ value "."
";
}
In php, iterative traversal mainly uses the following functions.
The current element of current () iteration.
Reset () moves to the first element and returns it.
Next () moves to the next element and returns it.
Move prev () to the previous element and return it.
End () moves to the last element and returns it.
Each () returns the index and value of the current element in the form of an array and moves it to the next iteration.
Key () returns the current index.
Array _ walk () calls a function for each element.
Array _ reduce () is calculated in sequence for each element.
Flexible Traversal
Array_walk ($ test01, pai_test );
Function performance_test ($ key, $ value ){
Echo "walk: $ key => $ value "."
";
}
$ Test02 = array (1, 2, 3, 4, 5 );
Echo array_reduce ($ test02, performance_test );
Function performance_test ($ run_result, $ current_value ){
Return $ run_result + $ current_value * $ current_value;
}