Php traversal array foreacheach () list () method summary

Source: Internet
Author: User
Php traversal array foreacheach () list () method summary

  1. Foreach ($ array as $ value) {// $ array indicates the array to be traversed. $ value indicates the pointer pointing to the current value of the array. as assigns a value.
  2. Code to executed;
  3. }

The foreach statement can also obtain the key name of the array, as shown below:

  1. Foreach ($ array as $ key => $ value ){
  2. Echo $ key "-" $ value ."
    ";
  3. }

2. the echo () function each () is used to split the key-value pairs of the current element of the array into a new array and use the next element as the current element. For example, Array (..., 'Robert '=> 'Bob ',...) in 'Robert '=> 'Bob', split it into Array ([1] => 'Bob', [value] => 'Bob ', [0] => 'Robert ', [key] => 'Robert') array, split into two sets (four key-value pairs in total), return, number pairs of 0 and 1, key and value name-value pairs. you can use one of them.

Example:

  1. $ Prices = Array ('trigger' => 100, 'Oil '=> 10, 'spank S s' => 4 );
  2. While ($ elements = each ($ prices )){
  3. Echo $ elements ['key']; // echo $ elements [0];
  4. Echo "-";
  5. Echo $ elements ['value']; // echo $ element [1];
  6. Echo"
    ";
  7. }

Output result: Tires-100Oil-10Spank Plugs-4

3. the list () function traverses an array. the list () function can be used to break an array into a series of values. List () is often used with each. However, list () can be used together with each (), for example, list ($ key, $ value) = explode (":", $ v); list ($ key, $ value) = each ($ array); // $ key, $ value can be any name of the variable name. in this statement, the elements at the current 0, 1 position of the array returned by each are assigned to $ key, $ value variable. Example:

  1. $ Prices = Array ('trigger' => 100, 'Oil '=> 10, 'spank S s' => 4 );
  2. While (list ($ product, $ prices) = each ($ prices );){
  3. Echo $ product "-" $ prices;
  4. Echo"
    ";
  5. }

Output result: Tires-100Oil-10Spank Plugs-4

Another implementation method:

  1. $ Prices = Array ('trigger' => 100, 'Oil '=> 10, 'spank S s' => 4 );
  2. List ($ product, $ price) = $ each ($ prices );
  3. Echo "$ product-$ price"; // output the first array
  4. $ Next = $ next ($ prices); // move the pointer behind
  5. Echo $ next;

Related Article

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.