Foreach () usage in PHP _ PHP Tutorial

Source: Internet
Author: User
Summary of foreach () usage in PHP. Summary of foreach () usage in PHP this article mainly introduces the usage of foreach () in PHP and related examples in detail. For more information, see. PHP4 introduction to foreach () usage in PHP

This article mainly introduces the foreach () usage and related examples in PHP in detail. it is very meticulous. if you need it, you can refer to it.

PHP 4 introduces the foreach structure, which is similar to Perl and other languages. This is just a simple way to traverse arrays. Foreach can only be used for arrays. an error occurs when you try to use it for another data type or an uninitialized variable. There are two types of syntax. The second type is secondary, but it is the first type of useful extension.

?

1

2

3

4

5 foreach (array_expression as $ value)

Statement

Foreach (array_expression as $ key => $ value)

Statement

The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

The second format does the same thing, except that the key name of the current unit is assigned to the variable $ key in each loop.

First, let's look at the first statement. this statement is relatively simple. array_expression refers to an array expression. the as $ val statement obtains the values of this array in sequence and saves them to the $ val variable, in this method, only values in the array can be obtained, but the subscript index value of the array cannot be obtained. For example:

?

1

2

3

4 $ myArray = array ("1" => "val1", "2" => "val2", "3" => "val3 ");

Foreach ($ myArray as $ val ){

Print ($ val ."");

}

The result will be output: val1 val2 val3

Let's take a look at the second format. in addition to getting the values of elements in the array, the second format can also get the index values of elements and save them to the $ key variable, if the index value of the array is not manually set, the default value is returned,

Look at the positive example:

Let's first look at a simple one-dimensional array:

?

1

2

3

4 $ myArray = array ("1" => "val1", "2" = "val2", "3" => "val3 ");

Foreach ($ myArray as $ key => $ val ){

Print ($ key. "=>". $ val .";");

}

The program will output: 1 => val1; 2 => val2; 3 => val3;. Next let's look at a complex two-dimensional array traversal. The program is as follows:

?

17 $ myArray = array (

"1" => array ("11" => "val11", "12" => "val12", "13" => "val13 "),

"2" => array ("21" => "val21", "22" => "val22", "23" => "val23 "),

"3" => array ("31" => "val31", "32" => "val32", "33" => "val33 ")

);

Print ("

    ");

    Foreach ($ myArray as $ key => $ val ){

    Print ("

  • ". $ Key ."
  • ");

    If (is_array ($ val) {// checks whether the $ val value is an array. if yes, it goes to the lower-layer traversal.

    Print ("

      ");

      Foreach ($ val as $ key => $ val ){

      Print ("

    • ". $ Key." => ". $ val ."
    • ");

      }

      Print ("

    ");

    }

    }

    Print ("

");

Output result:

?

121

11 => val11

12 => val12

13 => val13

2

21 => val21

22 => val22

23 => val23

3

31 => val31

32 => val32

33 => val33

  

    And
  • A label is used to display solid and hollow dots.

    Because the above is a two-dimensional array, the $ val value obtained after the first traversal will be an array, so I added a judgment in the traversal to traverse the two-layer array.

    Use an instance to solve the problem.

    ?

    $ A = array ("1" => "Chinese", "2" => "mathematics", "3" => "English ");

    $ B = array ("1" => "95", "2" => "99", "3" => "92 ");

    Foreach ($ a as $ key => $ value ){

    Echo $ value;

    Echo $ B [$ key]."
    ";

    }

    ?>

    The question is, why should I use $ B [$ key] instead of $ B [$ value] for the value in the output array $ B?

    Why?

    $ A = array ("1" => "Chinese", "2" => "mathematics", "3" => "English ");

    The above and below are exactly the same

    $ A [1] = "Chinese ";

    $ A [2] = "mathematics"

    $ A [3] = "English"

    How do we output the above array?

    It must be echo $ a [1];

    Right?

    If you have no questions, continue !!!!

    ------------------------------

    Foreach

    The format is foreach (array name as subscript => value)

    The subscript is the above $ a [1]. here 1 is the subscript of the array!

    Now you should understand why $ a [$ key] is output.

    Remember, no matter how it changes, the array output method will always be $ a [1], not $ a ['China']

    ========================================================== ======================================

    Foreach () has two usage methods:

    ?

    1

    2

    31: foreach (array_name as $ value ){

    Statement;

    }

    Here, array_name is the name of the array to be traversed. in each loop, the value of the current element of the array_name array is assigned to $ value, and the downloading inside the array is moved one step down, that is, the next element is returned in the next loop.

    ?

    1

    2

    32: foreach (array_name as $ key => $ value ){

    Statement;

    }

    The difference between this method and the first method is that there is an extra $ key, that is, in addition to assigning the value of the current element to $ value, the key value of the current element is also assigned to the variable $ key in each loop. The key value can be either a lower mark value or a string. For example, "0" in book [0] = 1 and "id" in book [id] = "001 ".

    The above is all the content of this article. I hope you will like it.

Summary of future () usage this article mainly introduces the foreach () usage and related examples in PHP in detail. it is very meticulous. if you need it, you can refer to it. PHP 4 introduction...

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.