4

I have $array=array("nothing","1","2","3","4","5","6","7","8","9","10"); and a piece of code as a template:

$var=<<<EOF

$array[0]
<div>
  <!-- something else -->
  <ul>
    <li>$array[1]</li>
    <li>$array[2]</li>
    <li>$array[3]</li>
  </ul>
  <!-- something else -->
</div>

EOF;

I want dynamically repeat and modify this HTML code while there are array elements. I mean this result:

$var=<<<EOF

$array[0]
<div>
  <!-- something else -->
  <ul>
    <li>$array[1]</li>
    <li>$array[2]</li>
    <li>$array[3]</li>
  </ul>
  <!-- something else -->
</div>
<div>
  <!-- something else -->
  <ul>
    <li>$array[4]</li>
    <li>$array[5]</li>
    <li>$array[6]</li>
  </ul>
  <!-- something else -->
</div>
<div>
  <!-- something else -->
  <ul>
    <li>$array[7]</li>
    <li>$array[8]</li>
    <li>$array[9]</li>
  </ul>
  <!-- something else -->
</div>
<div>
  <!-- something else -->
  <ul>
    <li>$array[10]</li>
  </ul>
  <!-- something else -->
</div>

EOF;
1
  • 3
    Check out the manual entry on foreach loops. Commented Jul 20, 2012 at 13:41

2 Answers 2

2

Expanding on @Nadav's answer and making it better:

<?php
$array = array(0);

for ($i = 1; $i < count($array); ): ?>
<div>
  <!-- something else -->
  <ul>
    <li><?php echo $array[$i++] ?></li> // i == 1, 4, 7, ....
    <li><?php echo $array[$i++] ?></li> // i == 2, 5, 8, ....
    <li><?php echo $array[$i++] ?></li> // i == 3, 6, 9, ....
  </ul>
  <!-- something else -->
</div>
<?php endfor; ?>

But then the above will fail if 2,3 dont exist or 5,6 don't exist, etc.

Update:

This code will work correctly:

<?php

$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 22);

for ($i = 1; $i < count($array);): ?>
<div>
  <!-- something else -->
  <ul>
    <?php for($j = 0; $j < 3 && $i + $j < count($array); ++$j):?>
    <li><?php echo $array[$j + $i] ?></li>
    <?php endfor; $i += $j;?> 
  </ul>
  <!-- something else -->
</div>
<?php endfor; ?>

DEMO: http://codepad.org/LFGMkd1G

Sign up to request clarification or add additional context in comments.

7 Comments

@YNT I have the 2nd part working. I am updating and putting DEMO
@YNT I updated my answer with a working answer and demo. Please let me know if you need a code explanation.
It works and this is the best explanation. This code is very complicated for me; however, I can copy-past :-).
@YNT break it down, bit by bit, I am sure you will get it eventually :-)
I have to because it is very interesting to learn programming.
|
1
<?php
$array[0];

for ($i = 0; $i <= count($array); $i + 3) {
?>
<div>
  <!-- something else -->
  <ul>
    <li><? $array[$i + 1] ?></li>
    <li><? $array[$i + 2] ?></li>
    <li><? $array[$i + 3] ?></li>
  </ul>
  <!-- something else -->
</div>
<?php } ?>

The loop you wanted isn't standard, so can only hope it works for you.

Basically it start with $i = 0, and prints $i + 1(= 1), (=2), (=3), next round $i equals 3 ($i + 3), and prints $i + 1(=3+1=4), (=5) , (=6), and so on, until the array reaches its end.

2 Comments

Note that with this code you will loose the last one or two elements if the number of elements in the array is not divisible by 3 (e.g. if you have 5 elements, you'll print only the first 3)
@Nadav Thanks for your answer, I'll try to apply this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.