Code for manipulating arrays with foreach in PHP _php tutorial

Source: Internet
Author: User
There are two ways to use foreach ():
Copy CodeThe code is as follows:
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.
Copy CodeThe code is as follows:
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".
program Example 1:
Copy CodeThe code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach Example 1:value only */
echo "foreach example 1:value only".
';
$a = Array (1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of". $a. ":". $v. "
";
}
?>
Run results
foreach Example 1:value only
Current value of $a: 1
Current value of $a: 2
Current value of $a: 3
Current value of $a: 17

2
Copy CodeThe code is as follows:
/*-------------------------------------------------------------------------*/
/* foreach example 2:value (with key printed for illustration) */
Echo '
'.'
'." foreach example 2:value (with key printed for illustration) "."
';
$a = Array (1, 2, 3, 17);
$i = 0; /* For illustrative purposes only */
foreach ($a as $v) {
echo "" $a [$i] = $v "."
';
$i + +;
}
Program Run Results
foreach example 2:value (with key printed for illustration)
$a [0] = 1
$a [1] = 2
$a [2] = 3
$a [3] = 17

3
Copy CodeThe code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach example 3:key and value */
Echo '
'.'
'." foreach Example 3:key and value "."
';
$a = Array (
"One" = 1,
"Both" = 2,
"Three" = 3,
"Seventeen" = 17
);
foreach ($a as $k = = $v) {
echo "" $a [$k] = $v "."
';
}
Program Run Results
foreach Example 3:key and value
$a [one] = 1
$a [both] = 2
$a [Three] = 3
$a [Seventeen] = 17

4
Copy CodeThe code is as follows:
/*-------------------------------------------------------------------------*/
/* foreach Example 4:multi-dimensional arrays */
Echo '
'.'
'." foreach Example 4:multi-dimensional arrays ". '
';
$a = array ();
$a [0][0] = "a";
$a [0][1] = "B";
$a [1][0] = "Y";
$a [1][1] = "Z";
foreach ($a as $v 1) {
foreach ($v 1 as $v 2) {
echo "$v 2" n ";
}
}
Program Run Results
foreach Example 4:multi-dimensional arrays
A b y Z

5
Copy CodeThe code is as follows:
/*-------------------------------------------------------------------------*/
/* foreach Example 5:dynamic arrays */
Echo '
'.'
'." foreach Example 5:dynamic arrays ". '
';
foreach (Array (1, 2, 3, 4, 5) as $v) {
echo "$v" n ";
}
Program Run Results
foreach Example 5:dynamic arrays
1 2 3) 4 5

You can also use this:
Copy CodeThe code is as follows:
$messageNav [' home '] =root_path;
$messageNav [' talent exchange '] = "#"
$messageNav [' dynamic column '] = "hragent/cn/"
$value):?>

"> >

"Class=" > "Onlink"


http://www.bkjia.com/PHPjc/324013.html www.bkjia.com true http://www.bkjia.com/PHPjc/324013.html techarticle foreach () has two uses: the copy code is as follows: foreach (Array_Name as $value) {statement;} here Array_Name is the name of the array you want to traverse, each time the loop, the Array_Name array ...

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