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.