将字符串列表拆分为类似长度的新列表

Say I have a list of 65 strings.
I need to split this single list into multiple "pools" that have a similar amount of strings.
The amount cannot be over 32.

In this list of 65, they're all ranked from 1st to 65th.

  • For a list of 65 strings, it'd split into one pool of 21, and two pools of 22.
  • For a list of 34 strings, it'd split into one pool of 18, and one pool of 18.
  • For a list of 115 strings, it'd split into one pool of 28, and three pools of 29.

And so on.

However, the new lists need to be fairly ranked.
In example it should be like so:

  • rank 1 in pool 1
  • rank 2 in pool 2
  • rank 3 in pool 3
  • rank 4 in pool 1
  • rank 5 in pool 2
  • rank 6 in pool 3

This way, rank 1 and rank 4, become rank 1 and 2 in their new list.
Same goes for the rest.

I'm thinking I'd need to use array_chunk in combination with a modulo operation, but I can't seem to wrap my head around it.

This is not as difficult as it might seem:

// settings
$cellCount   = 115;
$maxPoolSize = 32;

// create test array with numbered strings
$testArray = array_fill(1,$cellCount,'Cell ');
foreach ($testArray as $key => $value) $testArray[$key] .= $key;

// determine the number of pools needed
$arraySize   = count($testArray);
$poolCount   = ceil($arraySize/$maxPoolSize);

// fill the pools
$poolNo = 0;
foreach ($testArray as $cell)
{
  $poolArray[$poolNo][] = $cell;
  $poolNo++;
  if ($poolNo == $poolCount) $poolNo = 0;
}

// show result
echo '<pre>';
print_r($poolArray);
echo '</pre>';

I'm sure there are other solutions, but this seems to do the job.

I think you're on the right way, by using array_chunk and a modulo operation, that's the way i would choose.

It would be like:

$countarray = count($myarray);
$modulo = 2;
while ($countarray>32)
{
    $result = $countarray/$modulo;
    if($result>32)
    $modulo++;

}
$newpool = array_chunk($myarray, $modulo);

I'm not a god in php, so i hope it will help ! Sorry for my poor english.