<?php
function restructure($x,$slots)
{
$result=array();
$count=count($x);
$least=(int)($count/$slots);
$excess=$count-$least*$slots;
for($i=0;$i<($least+1)*$excess;$i+=$least+1)
array_push($result,implode(", ",array_slice($x,$i,$least+1)));
for(;$i<$count;$i+=$least)
array_push($result,implode(", ",array_slice($x,$i,$least)));
return $result;
}
$x=array(
"item 1",
"item 2",
"item 3",
"item 4",
"item 5",
"item 6",
"item 7",
);
print_r(restructure($x,5));
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Array
(
[0] => item 1, item 2
[1] => item 3, item 4
[2] => item 5
[3] => item 6
[4] => item 7
)
preferences:
143.36 ms | 407 KiB | 5 Q