PHP learning series seven database operation arrays: $ productarray (tires, oil, sparkplugs); array is a language structure, not a function. $ Product [0], $ product [1] foreach ($ produceas $ current) {echo $ current.;} associated array: $ pricesarray (PHP Learning Series 7
Database operations
Array:
$ Product = array ('tires', 'Oil ', 'spark S s ');
Array is a language structure, not a function.
$ Product [0], $ product [1]
Foreach ($ produce as $ current ){
Echo $ current ."";
}
Join array:
$ Prices = array ('tires '=> 100, 'Oil' => 10, 'spark S s' => 4)
Access: $ prices ['tires']
Traversal: foreach or list () and each () structures
Foreach ($ prices as $ key => $ value ){
Echo $ key. "-". $ value ."
";
}
While ($ element = each ($ prices )){
Echo $ element ['key'];
Echo "-";
Echo $ element ['value'];
Echo"
";
}
While (list ($ product, $ price) = each ($ prices )){
Echo "$ product-$ price
";
}
List () converts the elements 0 and 1 in the array returned by each () into two new variables named $ product and $ price.
If you want to use the array twice in the same script, you must use the reset () function to reset the current element to the beginning of the array. Traverse again, using the following:
Reset ($ prices );
While (list ($ product, $ price) = each ($ prices )){
Echo "$ produce-$ price
";
}
Sort (): array sorting. case sensitive. Uppercase letters are all in front of lowercase letters.
Asort ()
Ksort (): sorts joined arrays.
Asort () is sorted by each element value. Ksort () is sorted by keywords.
Reverse sorting: rsort (), arsort (), krsort ().
Use usort () to tell php how to compare various elements, you need to write your own comparison functions. It is equivalent to implementing the compareTo () function in java.
Function compare ($ x, $ y ){
If ($ x [1] ==$ y [1]) {
Return 0;
} Else if ($ x [1] <$ y [1]) {
Return-1;
} Else {
Return 1;
}
}
Usort ($ product, 'Company ');
If you want to store arrays in another order, you only need to write a different comparison function.
Uasort (), uksort ()