In php, arrays are divided into one-dimensional arrays and multi-dimensional arrays. if we want to loop two-dimensional arrays, we can directly use foreach and for to traverse them. below I will introduce several two-dimensional array loops.
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 loops traverse two-dimensional arrays,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 the array cyclically. each time a loop is executed, the value of the current array element is assigned to the value variable (the array pointer is moved one by one). 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;
- }
- }