views:

31

answers:

1

Hi

I have a array with ID values like this [2,34,7,46,16,19 ...] etc

each ID points to more data which I display with html like:

<ul>
  <li>
    content from 2
    content from 34
    content from 7
  </li>

  <li>
    content from 46
    content from 16
    content from 19
  </li>
  ...

notice that in this case there are 3 entries into each list item. How can share entries from the array to each <li> in a such way that there are always exactly 5 list items?

for eg, if the array has 15 items, each <li> has 3 items, or if array has 9 items, each <li> has at least 1 entry (4 of them will have 2)

A: 

array_chunk($myArray, count($myArray) / 5) should work. You might need to tweak it a little, such as a ceil or floor on the count/5, but that's the right direction.

http://php.net/manual/en/function.array-chunk.php

Matt Williamson