Search code examples
phparraysgrouping

Display 3 array elements per div tag


I have a problem creating a module in joomla. My php skills are restricted. I use foreach to show elements in the template file:

<?php foreach ($filtered_array as $index => $value) { ?>
    <div>show information></>
<?php } ?>

How can I use array_chunk() to group 3 elements into one div and the next 3 into another div?


Solution

  • Just use array_chunk as you told and a nested loop.

    <?php foreach (array_chunk($filtered_array, 3) as $row): ?>
        <?php foreach ($row as $value): ?>
            <div>show information></div>
        <?php endforeach; ?>
    <?php endforeach; ?>