Given: two arrays. In the first 8 letters, in the second 8 digits.
Task: Print the first 4 letters, then print the first 4 numbers, then print the last 4 letters and at the end print the last 4 numbers.
Condition: You can not use any cycles, except foreach. You cannot change arrays.
Those. output should be: abcd 1 2 3 4 efgh 5 6 7 8
I did this:
<?php $letters = ['a','b','c','d','e','f','g','h']; $numbers = ['1','2','3','4','5','6','7','8']; $outside = 0; $inside = 0; foreach($letters as $letter) { if($outside < 4) { echo "$letter\n"; } if($outside == 4) { foreach($numbers as $number) { if($inside < 4) { echo "$number\n"; } $inside++; } $inside = 0; } if($outside > 3) { echo "$letter\n"; } if($outside == 7) { foreach($numbers as $number) { if($inside > 3) { echo "$number\n"; } $inside++; } } $outside++; } But it seems to me that this code looks like a barn and can be more elegant. Tell me a normal solution, please make a code review for me.