1

I have an array of unknown length:

$array = array('1', '2', '3', '4', '5', '6', '7', '8' ...);

I need to output this array as multiple lists, where a new list is created for every 3 array entries.

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

<ul>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ul>

<ul>
  <li>7</li>
  <li>8</li>
</ul>

Note that I need to close the last list even if it does not contain 3 list-items.

Here is my attempt:

<?php for ($i = 0; $i < count($rows); ++$i): ?>

  <?php if (($i % 3) == 0): ?>
    <ul>
  <?php endif; ?>
    <li><?php print $rows[$i]; ?></li>
  <?php if (($i % 3) == 2): ?>
    </ul>
  <?php endif; ?>

<?php endfor; ?>
2
  • 4
    I have tried show us your attempt Commented Feb 18, 2015 at 8:40
  • 1
    I think now you got various answers and all are very interesting, go trough each answer and choose one which works for you! Commented Feb 18, 2015 at 8:49

7 Answers 7

5

You could utilize an array_chunk for this task. Chunk them by threes:

$array = range(1, 8);
$unknown_length = array_chunk($array, 3); // cut by batches of three
foreach($unknown_length as $ul) {
    echo '<ul>';
    foreach($ul as $li) {
        echo "<li>$li</li>";
    }
    echo '</ul>';
}

Sample Output

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

2 Comments

array_chunk and very meaningful variable names that's a good example of code
@Rizier123 i didn't thought about it actually, just first thing that popped out of my head while typing. lol :D.
3

This should work for you:

(Here I split the array in chunks of 3 with array_chunk(). Then I loop through each innerArray with a foreach loop and print the lists with implode())

<?php

    $array = array('1', '2', '3', '4', '5', '6', '7', '8');

    foreach(array_chunk($array, 3) as $chunk) 
        echo "<ul><li>" . implode('</li><li>', $chunk) . "</li></ul>";

?>

Comments

1

Try with foreach loop

$array = array('1', '2', '3', '4', '5', '6', '7', '8');
$i=1;
foreach($array as $a) {
  if($i == 1) echo '<ul>';
  echo '<li>'.$a.'</li>';
  if($i % 3 == 0) {
    echo '</ul>';
    echo '<ul>';
  }
  if($i == count($array))
    echo '</ul>';
  $i++;
}

output :-

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<ul>
<li>7</li>
<li>8</li>
</ul>

Comments

1
$i = 1;
foreach($array as $value){
   switch($i){
      case 1:
         echo '<ul>' 
               . '<li>' . $value . '</li>';
         break;
      case 2:
         echo '<li>' . $value . '</li>';
         break;
      case 3:
         echo '<li>' . $value . '</li>'
              . '</ul>';
         $i = 0;
         break;
   }
   $i++;
}

2 Comments

Okay one thing i have to admit here. You wrote the only answer i never would have thought of and i didn't expected it, but it's a very creative solution.
Thanks! It's not very advanced or structural complex, but it works and is simple to understand. @Rizier123
0
$array = array('1', '2', '3', '4', '5', '6', '7', '8');
echo "<ul>";
foreach($array as $key => $val){

    echo "<li>$val</li>";

    if(($key+1)%3 == 0){
        echo "</ul><ul>";       
    }
}
echo "</ul>";

Demo

Comments

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

        $n = count($array);
        $i=1;
        $list = '';
        foreach ($array as $val) {

            if($i==1){
                $list .='<ul>';
            }
                $list .='<li>'.$val.'</li>';
            if($i%3==0){
                $list .='</ul>';
                $list .='<ul>';
            }

            if($n==$i){ $list .='</ul>'; }
            $i++;
        }
        echo $list;

Comments

0

Why not to use template-like alternative loop syntax right in HTML? (of course if you are using some kind of "views")

<? for ($i=0; $i<count($a); $i+=3): ?>
    <ul>
    <? for ($j=$i; $j<min(count($a), $i+3); $j++): ?>
        <li><? echo $a[$j]; ?></li>
    <? endfor; ?>
    </ul>
<? endfor; ?>

Comments

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.