php 将数组拆分为特定数量的块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15723059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split array into a specific number of chunks
提问by SnitramSD
I know that array_chunk()
allows to split an array into several chunks, but the number of chunks changes according to the number of elements. What I need is to always split the array into a specific number of arrays like 4 arrays for example.
我知道这array_chunk()
允许将数组拆分为几个块,但是块的数量根据元素的数量而变化。我需要的是始终将数组拆分为特定数量的数组,例如 4 个数组。
The following code splits the array into 3 chunks, two chunks with 2 elements each and 1 chunk with 1 element. What I would like is to split the array always into 4 chunks, no matter the number of total elements that the array has, but always trying to divide the elements evenly in the chunks like the array_chunck function does. How can I accomplish this? Is there any PHP function for this?
以下代码将数组拆分为 3 个块,两个块各有 2 个元素,1 个块有 1 个元素。我想要的是将数组始终拆分为 4 个块,无论数组具有的总元素数是多少,但始终尝试像 array_chunck 函数那样将元素均匀地划分在块中。我怎样才能做到这一点?是否有任何 PHP 函数?
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
Thank you.
谢谢你。
回答by Baba
You can try
你可以试试
$input_array = array(
'a',
'b',
'c',
'd',
'e'
);
print_r(partition($input_array, 4));
Output
输出
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
)
[2] => Array
(
[0] => d
)
[3] => Array
(
[0] => e
)
)
Function Used
使用的功能
/**
*
* @param Array $list
* @param int $p
* @return multitype:multitype:
* @link http://www.php.net/manual/en/function.array-chunk.php#75022
*/
function partition(Array $list, $p) {
$listlen = count($list);
$partlen = floor($listlen / $p);
$partrem = $listlen % $p;
$partition = array();
$mark = 0;
for($px = 0; $px < $p; $px ++) {
$incr = ($px < $partrem) ? $partlen + 1 : $partlen;
$partition[$px] = array_slice($list, $mark, $incr);
$mark += $incr;
}
return $partition;
}
回答by Jakob Pogulis
Divide the size of the array with the number of chunks you want and supply that as the size of each chunk.
将数组的大小除以所需的块数,并将其作为每个块的大小提供。
function array_chunks_fixed($input_array, $chunks) {
if (sizeof($input_array) > 0) {
$chunks = 3;
return array_chunk($input_array, intval(ceil(sizeof($input_array) / $chunks)));
}
return array();
}
回答by Jamie Chong
Another implementation similar to @Baba's partition()
function.
另一个类似于@Babapartition()
函数的实现。
// http://php.net/manual/en/function.array-slice.php#94138
// split the given array into n number of pieces
function array_split($array, $pieces=2)
{
if ($pieces < 2)
return array($array);
$newCount = ceil(count($array)/$pieces);
$a = array_slice($array, 0, $newCount);
$b = array_split(array_slice($array, $newCount), $pieces-1);
return array_merge(array($a),$b);
}
// Examples:
$a = array(1,2,3,4,5,6,7,8,9,10);
array_split($a, 2); // array(array(1,2,3,4,5), array(6,7,8,9,10))
array_split($a, 3); // array(array(1,2,3,4), array(5,6,7), array(8,9,10))
array_split($a, 4); // array(array(1,2,3), array(4,5,6), array(7,8), array(9,10))
回答by Andrew Surdu
So many complex answers here. I'll post my simple solution :)
这里有很多复杂的答案。我会发布我的简单解决方案:)
function splitMyArray(array $input_array, int $size, $preserve_keys = null): array
{
$nr = (int)ceil(count($input_array) / $size);
if ($nr > 0) {
return array_chunk($input_array, $nr, $preserve_keys);
}
return $input_array;
}
Usage:
用法:
$newArray = splitMyArray($my_array, 3);
More details here:https://zerowp.com/split-php-array-in-x-equal-number-of-elements/
更多细节在这里:https : //zerowp.com/split-php-array-in-x-equal-number-of-elements/
回答by Sarthak Singhal
If someone is looking for a solution to divide the super array into smaller number of separate arrays.
如果有人正在寻找将超级阵列划分为较少数量的单独阵列的解决方案。
$count = count($appArray); //$appArray contains all elements
$repoCount = 3; //No. of parts to divide
$n = floor($count/$repoCount);
$rem = $count % $repoCount;
$j=1;
while($j <= $repoCount){
${"arr_" . $j} = array();
$j++;
}
$j=0;
$k=1;
for($i=0; $i < $count; $i++){
if($j < $n){
array_push(${"arr_" . $k}, $appArray[$i]);
$j++;
}
else if($k < $repoCount){
$j=0;
$k++;
--$i;
}
if($i >= ($count-$rem)){
$k=1;
for($j=0; $j < $rem; $j++, $i++, $k++){
array_push(${"arr_" . $k},$appArray[$i]);
}
break;
}
}
回答by MichaelRushton
This should work:
这应该有效:
function getChunks(array $array, $chunks)
{
if (count($array) < $chunks)
{
return array_chunk($array, 1);
}
$new_array = array();
for ($i = 0, $n = floor(count($array) / $chunks); $i < $chunks; ++$i)
{
$slice = $i == $chunks - 1 ? array_slice($array, $i * $n) : array_slice($array, $i * $n, $n);
$new_array[] = $slice;
}
return $new_array;
}
$input_array = array('a', 'b', 'c', 'd', 'e');
echo '<pre>' . print_r(getChunks($input_array, 4), TRUE) . '</pre>';
回答by Alireza Aboutalebi
This what i write and work well print_r(array_divide($input,3));
这就是我写的并且工作得很好 print_r(array_divide($input,3));
function array_divide($array, $segmentCount) {
$dataCount = count($array);
if ($dataCount == 0) return false;
$segmentLimit = 1;
//if($segmentCount > $segmentLimit)
// $segmentLimit = $segmentCount;
$outputArray = array();
$i = 0;
while($dataCount >= $segmentLimit) {
if( $segmentCount == $i)
$i = 0;
if(!array_key_exists($i, $outputArray))
$outputArray[$i] = array();
$outputArray[$i][] = array_splice($array,0,$segmentLimit)[0] ;
$dataCount = count($array);
$i++;
}
if($dataCount > 0) $outputArray[] = $array;
return $outputArray;
}