I have an array which can have several items in it, e.g:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
etc
I need the quickest way to restructure this array so that it has at most X items. So if I say that X is 3, the resulting array must be:
Item 1 , Item 2
Item 3, Item 4
Item 5, Item 6
etc
or if it has 7 items, it would be:
Item 1 , Item 2, Item 3,
Item 4, Item 5,
Item 6, Item 7
What's the easiest way to do this?
I started with this, but it seems like there really must be an easier way:
foreach ($addressParts as $part)
{
if (empty($part)) continue;
if (empty($addressLines[$count])) $addressLines[$count] = '';
$addressLines[$count] .= $part;
$count++;
if ($count > 2) $count = 0;
}
Also, this won't work, because you will end up with this:
item 1, item 4, item 7
item 2, item 5
item 3, item 6
... which is wrong. Any thoughts?
UPDATE
If I start with:
Array
(
[0] => item 1
[1] => item 2
[2] => item 3
[3] => item 4
[4] => item 5
[5] => item 6
[6] => item 7
)
I want to end with:
Array
(
[0] => item 1, item 2, item 3
[1] => item 4, item 5
[2] => item 6, item 7
)
Make sense?
variable unsigned integer
the array passed is of unknown length, and it has to be broken up sequentially following what rules exactly? ifX=3
break into three subarrays containing an equal amount of elements, ifX=7
break into... I don't get it. Either 3 and 7 are the only options available, and those 2 examples are the only result posible, or my brain is going to burn in derp – Cafard