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 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:

  1. Print_r ($ order_data_info );
  2. Array
  3. (
  4. [0] => stdClass Object ([countOrder] => 3 [amntTotal] => 2380.00 [name] => test version)
  5. [1] => stdClass Object ([countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP Edition)
  6. [2] => stdClass Object ([countOrder] => 2 [amntTotal] => 4760.00 [name] => Personal Edition)
  7. )
  8.  
  9. $ Order_length = sizeof ($ order_data_info );
  10. For ($ I = 0; $ I <$ order_length; ++ $ I)
  11. {
  12. $ Order_data_info [$ I]-> amntTotal = money ($ order_data_info [$ I]-> amntTotal );
  13. }
  14. Print_r ($ order_data_info );
  15. Array
  16. (
  17. [0] => Array ([countOrder] => 3 [amntTotal] => 2380.00 [name] => test version)
  18. [1] => Array ([countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP Edition)
  19. [2] => Array ([countOrder] => 2 [amntTotal] => 4760.00 [name] => Personal Edition)
  20. )
  21. $ Order_length = sizeof ($ order_data_info );
  22. For ($ I = 0; $ I <$ order_length; ++ $ I)
  23. {
  24. $ Order_data_info [$ I] ['amnttotal'] = money ($ order_data_info [$ I] ['amnttotal']);
  25. }

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:

  1. Foreach ($ array as $ key)
  2. {
  3. If (xxxx)
  4. {
  5. Break; // The break terminates the loop.
  6. }
  7. If (zzz)
  8. {
  9. Continue; // continue terminates this loop and enters the next loop at the moment;
  10. }
  11. }

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.