PHP foreach () Usage and examples

Source: Internet
Author: User
Tags php foreach

Original URL:
http://blog.csdn.net/china_skag/article/details/6444727

PHP 4 introduces a foreach structure, much like Perl and other languages. This is just a simple way to iterate through an array. foreach can only be used with arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of syntax, and the second is a useful extension that is minor but the first.

foreach (array_expression as $value)
Statement

foreach (array_expression as $key = $value)
Statement

The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).

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



The first statement, this statement is simple, array_expression refers to an array expression, as $val statement will take the value of the array and save to the $val variable, this method can only get the values in the array, but not the index value of the array subscript. For example:

$myArray =array ("1" = "Val1", "2" = "Val2", "3" = "Val3");
foreach ($myArray as $val) {
Print ($val. " ");
}

The result is output: Val1 val2 Val3


Take a look at 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.




One more example

<?php
$a = Array ("1" = "Chinese", "2" = "Math", "3" and "English");
$b = Array ("1" = "Up", "2" = "The", "3" = "92");
foreach ($a as $key = = $value) {
Echo $value;
echo $b [$key]. " <br> ";
}
?>

The question is why does the value in the output array $b be $b[$key] instead of $b[$value]?

What is this for?

$a = Array ("1" = "Chinese", "2" = "Math", "3" and "English");
And this one is exactly the same as the one below.

$a [1]= "language";
$a [2]= "math"
$a [3]= "English"

How do we output the above array?

It must be echo $a [1];

Right?

If there is no doubt we continue!!!!


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


Simply say foreach

Its format is such a foreach (array name as Subscript = = value)

The subscript is the top $a[1], where 1 is the subscript of the array!

Here you should understand, why is $a[$key] such output

You remember that no matter how it changes, the output method of the array is always $a[1], not $a[' language '.




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

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".

PHP foreach () Usage and examples

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.