Solve the php array loop problem

Source: Internet
Author: User
Please solve the php array loop problem. I have an array as follows:

$ Arr = array (1, 2, 3, 4, 5, 6, 7, 8 );
Expected cyclic output
Start1
1
2
3
4
End1
Start2
1
2
3
4
End2
----------------------------------------------------------
Start1
5
6
7
8
End1
Start2
5
6
7
8
End2

This means to group each four arrays.
In addition, each group must be repeated twice.


Reply to discussion (solution)

 ';foreach ($temp as $val) {echo $val.'
';}echo 'end'.$k.'
';}}

$ Arr = array (1, 2, 3, 4, 5, 6, 7, 8); $ n = 2; // loop twice $ split = 4; // 4 for one group echo'
';response($arr, $n, $split);echo '
'; Function response ($ arr, $ n, $ split) {while (count ($ arr)> 0) {$ tmp = array_splice ($ arr, 0, $ split ); for ($ I = 0; $ I <$ n; $ I ++) {echo 'start '. ($ I + 1 ). "\ r \ n"; echo implode ("\ r \ n", $ tmp ). "\ r \ n"; echo 'end '. ($ I + 1 ). "\ r \ n";} echo "---------------------------------------------------------- \ r \ n ";}}


start11234end1start21234end2----------------------------------------------------------start15678end1start25678end2

While (count ($ arr)> 0)
Why is it normal that the while loop is not terminated?

While (count ($ arr)> 0)
Why is it normal that the while loop is not terminated?



Array_splice removes $ arr elements. So when all elements are taken out. $ Arr is empty. exit the loop.

Thank you very much.

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.