Php two-dimensional array loop program code

Source: Internet
Author: User
In php, arrays are divided into one-dimensional arrays and multi-dimensional arrays. if we want to loop through two-dimensional arrays, we can directly use foreach and for traversal, next I will introduce you to the implementation of several two-dimensional array loops. if you need to know about them, please refer. For loop traversal II...

In php, arrays are divided into one-dimensional arrays and multi-dimensional arrays. if we want to loop through two-dimensional arrays, we can directly use foreach and for traversal, next I will introduce you to the implementation of several two-dimensional array loops. if you need to know about them, please refer.

For loop traversal of two-dimensional array

The code is as follows:

Print_r ($ order_data_info );

Array
(
[0] => stdClass Object ([countOrder] => 3 [amntTotal] => 2380.00 [name] => test version)
[1] => stdClass Object ([countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP Edition)
[2] => stdClass Object ([countOrder] => 2 [amntTotal] => 4760.00 [name] => Personal Edition)
)


$ Order_length = sizeof ($ order_data_info );
For ($ I = 0; $ I <$ order_length; ++ $ I)
{
$ Order_data_info [$ I]-> amntTotal = money ($ order_data_info [$ I]-> amntTotal );
}

Print_r ($ order_data_info );

Array
(
[0] => Array ([countOrder] => 3 [amntTotal] => 2380.00 [name] => test version)
[1] => Array ([countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP Edition)
[2] => Array ([countOrder] => 2 [amntTotal] => 4760.00 [name] => Personal Edition)
)

$ Order_length = sizeof ($ order_data_info );
For ($ I = 0; $ I <$ order_length; ++ $ I)
{
$ Order_data_info [$ I] ['amnttotal'] = money ($ order_data_info [$ I] ['amnttotal']);
}


The foreach statement is used to traverse arrays cyclically.

Each cycle is performed, the value of the current array element is assigned to the value variable (the array pointer is moved one by one)-and so on

The code is as follows:

Foreach ($ array as $ key)
{
If (xxxx)
{
Break; // The break terminates the loop.
}
If (zzz)
{
Continue; // continue terminates this loop and enters the next loop at the moment;
}
}

Example

The code is as follows:

$ Team = array ('LK ',' OK ');
$ Book = array ('Linux server configuration and management', $ team );

Foreach ($ book as $ k => $ val) // meaning for $ book each $ value ()
If (is_array ($ val) foreach ($ val as $ value) echo $ value .'
';
Else echo $ k. '=>'. $ val .'
';
?>

Example 2

The code is as follows:


$ Array = array ('saipan (symbian) '=> array (1 => 's60v3 horizontal version', 2 => 's60v3', 3 => 's60v5 '),
'Android (android) '=> array (4 => 'android2. 1 and later versions ', 6 => 'Android 1.5', 7 => 'Android 1.6 ', 8 => 'Android 2.1', 9 => 'Android 2.2 ', 10 => 'Android 2.3 ', 11 => 'Android 3.0', 12 => 'Android 3.1 ', 12 => 'Android 3.2', 12 => 'Android 4.0 ', 12 => 'Android 4.1 ', 12 => 'Android 4.2 '),
'Wp7 (Windowsphone) '=> array (5 => 'Windows Phone 7 system ')
);

Foreach ($ array as $ c => $ cc)
{
Echo''. $ C .'';
Foreach ($ cc as $ v => $ vv)
{
If ($ addRow ["sjos"] = $ v) {$ select = "selected";} else {$ select = "";}
Echo''. $ Vv .'';
}

}
?>



Tutorial address:

Reprinted! But please include the article address ^

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.