Use foreach in php to operate the array code

Source: Internet
Author: User

Foreach () has two usage methods:
Copy codeThe Code is as follows:
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 moves down one step, that is, the next element is returned in the next loop.
Copy codeThe Code is as follows:
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, "id" in book [id] = "001 ".
Program instance 1:
Copy codeThe Code is as follows:
<? Php
/*-------------------------------------------------------------------------*/
/* Foreach example 1: value only */
Echo "foreach example 1: value only". '<br/> ';
$ A = array (1, 2, 3, 17 );
Foreach ($ a as $ v ){
Echo "Current value of". $ a. ":". $ v. "<br/> ";
}
?>
// Running result
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 authentication )*/
Echo '<br/>'. '<br/>'. "foreach example 2: value (with key printed for authentication)". '<br/> ';
$ A = array (1, 2, 3, 17 );
$ I = 0;/* for each strative purposes only */
Foreach ($ a as $ v ){
Echo "" $ a [$ I] => $ v ". '<br/> ';
$ I ++;
}
// Program running result
Foreach example 2: value (with key printed for authentication)
$ 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 '<br/>'. '<br/>'. "foreach example 3: key and value". '<br/> ';
$ A = array (
"One" => 1,
"Two" => 2,
"Three" => 3,
"Seventeen" => 17
);
Foreach ($ a as $ k => $ v ){
Echo "" $ a [$ k] => $ v ". '<br/> ';
}
// Program running result
Foreach example 3: key and value
$ A [one] => 1
$ A [two] => 2
$ A [three] => 3
$ A [seventeen] => 17

4
Copy codeThe Code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach example 4: multi-dimen1_arrays */
Echo '<br/>'. '<br/>'. "foreach example 4: multi-dimen1_arrays". '<br/> ';
$ A = array ();
$ A [0] [0] = "";
$ A [0] [1] = "B ";
$ A [1] [0] = "y ";
$ A [1] [1] = "z ";
Foreach ($ a as $ v1 ){
Foreach ($ v1 as $ v2 ){
Echo "$ v2" n ";
}
}
// Program running result
Foreach example 4: multi-dimen1_arrays
A B y z

5
Copy codeThe Code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach example 5: dynamic arrays */
Echo '<br/>'. '<br/>'. "foreach example 5: dynamic arrays". '<br/> ';
Foreach (array (1, 2, 3, 4, 5) as $ v ){
Echo "$ v" n ";
}
// Program running result
Foreach example 5: dynamic arrays
1 2 3 4 5

This can also be used:
Copy codeThe Code is as follows:
$ MessageNav ['homepage'] = ROOT_PATH;
$ MessageNav ['talent communication'] = "#"
$ MessageNav ['dynamic column'] = "hragent/cn /"
<? Php $ I = 0; foreach ($ messageNav as $ key => $ value):?>
<? Php if ($ I! = Count ($ messageNav)-1):?>
<A href = "<? = $ Value?> "> <? = $ Key?> </A>
<? Php else:?>
<A href = "<? = $ Value?> "Class =" onlink "> <? = $ Key?> </A>
<? Php endif;?>
<? Php $ I ++; endforeach;?>

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.