将数组拆分为 N 个数组 - PHP

2022-08-30 16:35:00

我有一个包含18个值的数组:

$array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r');

我想将此数组拆分为12个不同的数组,因此它应如下所示:

array(
    0 => array('a', 'b'),
    1 => array('c', 'd'),
    2 => array('e', 'f'),
    3 => array('g', 'h'),
    4 => array('i', 'j'),
    5 => array('k', 'l'),
    6 => array('m'),
    7 => array('n'),
    8 => array('o'),
    9 => array('p'),
   10 => array('q'),
   11 => array('r')
)

我的函数似乎不起作用

function array_split($array, $parts){
    return array_chunk($array, ceil(count($array) / $parts));
}

$result = array_split($array, 12);

因为我得到了9个不同的数组而不是12个。它会回来的

array(
    0 => array('a', 'b'),
    1 => array('c', 'd'),
    2 => array('e', 'f'),
    3 => array('g', 'h'),
    4 => array('i', 'j'),
    5 => array('k', 'l'),
    6 => array('m', 'n'),
    7 => array('o', 'p'),
    8 => array('q', 'r')
)

我该怎么做?谢谢。


答案 1

这个简单的函数将为您工作:

用法

$array = range("a", "r"); // same as your array
print_r(alternate_chunck($array,12));

输出

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

    [3] => Array
        (
            [0] => g
            [1] => h
        )

    [4] => Array
        (
            [0] => i
            [1] => j
        )

    [5] => Array
        (
            [0] => k
            [1] => l
        )

    [6] => Array
        (
            [0] => m
        )

    [7] => Array
        (
            [0] => n
        )

    [8] => Array
        (
            [0] => o
        )

    [9] => Array
        (
            [0] => p
        )

    [10] => Array
        (
            [0] => q
        )

    [11] => Array
        (
            [0] => r
        )

)

更新以上可能对大多数情况没有用...这是另一种类型的块

$array = range("a", "r"); // same as your array
print_r(fill_chunck($array, 5));

输出

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [1] => Array
        (
            [0] => e
            [1] => f
            [2] => g
            [3] => h
        )

    [2] => Array
        (
            [0] => i
            [1] => j
            [2] => k
            [3] => l
        )

    [3] => Array
        (
            [0] => m
            [1] => n
            [2] => o
            [3] => p
        )

    [4] => Array
        (
            [0] => q
            [1] => r
        )

)

这将确保该组在任何时候都不会超过5个元素,而另一个元素没有限制

使用的函数

function alternate_chunck($array, $parts) {
    $t = 0;
    $result = array();
    $max = ceil(count($array) / $parts);
    foreach(array_chunk($array, $max) as $v) {
        if ($t < $parts) {
            $result[] = $v;
        } else {
            foreach($v as $d) {
                $result[] = array($d);
            }
        }
        $t += count($v);
    }
    return $result;
}


function fill_chunck($array, $parts) {
    $t = 0;
    $result = array_fill(0, $parts - 1, array());
    $max = ceil(count($array) / $parts);
    foreach($array as $v) {
        count($result[$t]) >= $max and $t ++;
        $result[$t][] = $v;
    }
    return $result;
}

答案 2

请允许我成为第一个提供基于数学的无循环解决方案的人。

数学中的魔力在于确定元素的哪一部分属于第一组块,其中所有列都在每行中填充,而哪些元素属于第二组(如果该集合甚至应该存在),其中除最右边的列之外的所有列都被填充。

让我画出我在说什么。标记两个分块数组之间的划分。>

$size = 9;        -------------    $size = 9;        -------------
$maxRows = 4;   1 | A , B , C |    $maxRows = 3;     | A , B , C |
$columns = 3;   > |-----------|    $columns = 3;   1 | D , E , F |
$fullRows = 1;    | D , E |        $fullRows = 3;    | G , H , I |
                2 | F , G |                        > -------------
                  | H , I |                        2      n/a
                  ---------


$size = 18;        ---------    $size = 17;       -------------------------------------
$maxRows = 12;     | A , B |    $maxRows = 2;   1 | A , B , C , D , E , F , G , H , I |
$columns = 2;      | C , D |    $columns = 9;   > -------------------------------------
$fullRows = 6;     | E , F |    $fullRows = 1;  2 | J , K , L , M , N , O , P , Q |
                 1 | G , H |                      ---------------------------------
                   | I , J |
                   | K , L |
                 > ---------
                   | M |
                   | N |
                   | O |
                 2 | P |
                   | Q |
                   | R |
                   -----

代码:(演示)

function double_chunk($array, $maxRows) {
    $size = count($array);
    $columns = ceil($size / $maxRows);
    $lessOne = $columns - 1;
    $fullRows = $size - $lessOne * $maxRows;
    
    if ($fullRows == $maxRows) {
        return array_chunk($array, $fullRows);  // all columns have full rows, don't splice
    }
    return array_merge(
               array_chunk(
                   array_splice($array, 0, $columns * $fullRows),  // extract first set to chunk
                   $columns
               ),
               array_chunk($array, $lessOne)   // chunk the leftovers
           );
}
var_export(double_chunk(range('a', 'i'), 3));

如果您不介意迭代的调用,则此操作更简短,并且可能更易于遵循(...也许不是):array_splice()

代码:(演示)

function custom_chunk($array, $maxRows) {
    $size = count($array);
    $columns = ceil($size / $maxRows);
    $lessOne = $columns - 1;
    $fullRows = $size - $lessOne * $maxRows;
    
    for ($i = 0; $i < $maxRows; ++$i) {
        $result[] = array_splice($array, 0, ($i < $fullRows ? $columns : $lessOne));
    }
    return $result;
}
var_export(custom_chunk(range('a', 'r'), 12));