For loop traversal of two-dimensional array
| The code is as follows: |
Copy code |
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: |
Copy code |
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: |
Copy code |
<? Php $ 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. '<br/> '; Else echo $ k. '=>'. $ val. '<br/> '; ?> |
Example 2
| The code is as follows: |
Copy code |
<? $ 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 '<option value = "'. $ c. '">'. $ c. '</option> '; Foreach ($ cc as $ v => $ vv) { If ($ addRow ["sjos"] = $ v) {$ select = "selected";} else {$ select = "";} Echo '<option value = "'. $ vv. '| '. $ v. '"'. $ select. '> & nbsp ;'. $ vv. '</option> '; } } ?> |