PHP Learning Series 7

Source: Internet
Author: User
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 ()






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.