Php traversal loop array implementation method

Source: Internet
Author: User
: This article describes how to implement the php traversal loop array. if you are interested in the PHP Tutorial, refer to it. Using foreach for list each while to traverse arrays, including common one-dimensional arrays and two-dimensional array traversal methods, the following describes in detail how to use each function.

  1. $ Foreach = array (1, 2, 3 );
  2. $ Array2 = array ('a' => 33, 'BB '=> 22, 'www .phpfensi.com' => 11), array ('DD' => 44, 'ee '=> 55, 'FF' => 66 ));

Use for foreach to traverse one-dimensional data:

  1. For ($ I = 0; $ I
  2. {
  3. Echo 'foreach ['. $ I.'] = '. $ foreach [$ I].'
    ';
  4. }
  5. /*
  6. // Foreach [0] = 1
  7. // Foreach [1] = 2
  8. // Foreach [2] = 3

Then we can use foreach to traverse:

  1. Foreach ($ foreach as $ v)
  2. {
  3. Echo 'foreach = '. $ v .'
    ';
  4. }
  5. // Foreach = 1
  6. // Foreach = 2
  7. // Foreach = 3

The above uses to traverse one-dimensional data. let's look at the two-dimensional array:

  1. Foreach ($ array2 as $ key => $ value)
  2. {
  3. Foreach ($ value as $ k => $ v)
  4. {
  5. Echo '$ arr ['. $ key. '] ['. $ k. '] ='. $ v;
  6. }
  7. Echo"
    ";
  8. }

Let's take a look at list each:

The each () function generates an array consisting of the key and key values of the elements pointed to by the current internal pointer of the array, and moves the internal pointer forward.

The returned array contains four elements: key name 0, 1, key, and value. Unit 0 and key contain the key name of the array unit, and 1 and value contain data.

If the internal pointer is out of the array range, this function returns false.

Syntax: each (array) parameter description

Array is required, specifying the array to be used.

List function

The list () function assigns values to a group of variables with elements in the array.

Note: Like array (), list () is actually a language structure, not a function.

Syntax: list (var1, var2. ..) parameter description

Var1 is required. The first variable to be assigned a value.

Var2 (optional) can contain multiple variables.

Tips and notes:

Note: This function is only used for the number index array, and it is assumed that the number index starts from 0.

  1. $ Colors = array ('red', 'blue', 'green', 'yellow ');
  2. While (list ($ key, $ val) = each ($ colors )){
  3. Echo "other list of $ val.
    ";
  4. }
  5. // Other list of red.
  6. // Other list of blue.
  7. // Other list of green.
  8. // Other list of yellow.

For statement:

If you have determined the number of times the code block is repeated, you can use the for statement.

Syntax:

  1. For (initialization; condition; increment)
  2. {
  3. Code to be executed;
  4. }

Note: the for statement has three parameters: the first parameter initializes the variable, the second parameter saves the condition, and the third parameter contains the increment required for the execution cycle, if the initialization or increment parameter contains multiple variables, use commas to separate them. the condition must be calculated as true or false.

The foreach statement is used to traverse arrays cyclically.

For each loop, the value of the current array element is assigned to the value variable (the array pointer is moved one by one)-and so on.

The above introduces the php traversal loop array implementation method, including the content, hope to be helpful to friends interested in PHP tutorials.

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.