PHP foreach Usages and instances

Source: Internet
Author: User

Original link https://www.cnblogs.com/DaBing0806/p/4717718.html

There are two ways to use foreach ():
1:foreach (Array_Name as $value) {
Statement
}
The array_name here is the name of the array you want to traverse, each time the value of the current element of the Array_Name array is assigned to $value, and the subscript inside the array moves down one step, that is, the next loop returns to the next element.


2:foreach (array_name as $key = $value) {
Statement
}
The difference between this and the first method is that there is a $key, that is, in addition to assigning the value of the current element to $value, the key value of the current element is assigned to the variable $key in each loop. The key value can be a subscript value or a string. For example, "0" in Book[0]=1, book[id]= "id" in "001".

To see the second format, the second format, in addition to the same as the first format to get the value of the elements in the array, you can also get the index value of the element, and save to the $key variable, if the index value of the array is not manually set, then return the system default setting value,
See positive examples:

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

$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 more complex two-dimensional array traversal, as follows:

$myArray =array (
"1" =>array ("One" and "Val11", "one" = "val12", "all" = "val13"),
"2" =>array ("+" = "val21", "all" = "val22", "all" = "val23"),
"3" =>array ("+" = "val31", "+" = "Val32", "val33")
);
Print ("<ul>");
foreach ($myArray as $key = $val) {
Print ("<li>". $key. " </li> ");
if (Is_array ($val)) {//Determines whether the value of $val is an array, and if so, goes to the downlevel traversal
Print ("<ul>");
foreach($val as $key = $val) {
Print ("<li>". $key. " = ". $val." </li> ");
}
Print ("</ul>");
}
}
Print ("</ul>");

Output Result:

    • 1
      • 11=>val11
      • 12=>val12
      • 13=>val13
    • 2
      • 21=>val21
      • 22=>val22
      • 23=>val23
    • 3
      • 31=>val31
      • 32=>val32
      • 33=>val33

<ul> and <li> are labels that show a solid small dot and a hollow small dot.
Since 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 for the two-layer array traversal.

PHP foreach Usages and instances

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.