将多个值拆分为两个或多个数组


Split multiple values into two or more arrays

例如,我的值=>1,2,3,4,5,6,7,8

我想得到这样的结果,1,2,3,45,6,7,8

我试过使用array_chunk,就像

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )
)

我不知道如何将[0][1][2][3]拆分或合并为一个数组[0]=>1,2,3,4和[1]=>5,6,7,8

我需要你的帮助,提前谢谢。

使用array_chunk后需要implode

$chunked = array_chunk([1,2,3,4,5,6,7,8], 4);
foreach($chunked as $chunk) {
     $imploded[] = implode(',', $chunk);
}
print_r($imploded); // Array ( [0] => 1,2,3,4 [1] => 5,6,7,8 )

您可以遍历生成的块并应用内爆函数

$values = array(1,2,3,4,5,6,7,8);
$newValues = array_chunk($values, 4);
array_walk(
  $newValues,
  function(&$chunk)
  {
    $chunk = implode(',', $chunk);
  }
);
print_r($newValues);

调用array_chunk后,可以使用array_map内爆每个子数组。

$result = array_map(function($subarray) {
    return implode(',', $subarray);
}, $chunked_array);