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

Splitting a list of strings into new lists of similar length

假设我有一个包含 65 个字符串的列表。
我需要将这个单个列表拆分为多个 "pools",它们具有相似数量的字符串。
金额不能超过32

在这 65 人的名单中,他们都排在第 1 到第 65 位。​​

以此类推

但是,新列表需要公平排名。
例如它应该是这样的:

这样,排名1和排名4,成为他们新榜单的排名1和2。
其他的也一样。

我想我需要将 array_chunk 与模运算结合使用,但我似乎无法理解它。

这并不像看起来那么难:

// 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>';

我确定还有其他解决方案,但这似乎可以解决问题。

我认为你的方法是正确的,通过使用 array_chunk 和模运算,这就是我会选择的方式。

就像:

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

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

本人不是php中的大神,希望对大家有所帮助!抱歉我的英语不好。