Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重组阵列的最快方法?_Php_Arrays_Multidimensional Array_Grouping_Implode - Fatal编程技术网

Php 重组阵列的最快方法?

Php 重组阵列的最快方法?,php,arrays,multidimensional-array,grouping,implode,Php,Arrays,Multidimensional Array,Grouping,Implode,我有一个数组,其中可以包含多个项目,例如: Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 etc 我需要最快的方法来重组这个数组,使它最多有X个项目。如果我说X是3,那么得到的数组必须是: Item 1 , Item 2 Item 3, Item 4 Item 5, Item 6 etc 或者,如果有7项,则为: Item 1 , Item 2, Item 3, Item 4, Item 5, Item 6, Item 7 最简单的方法是什么 我是从

我有一个数组,其中可以包含多个项目,例如:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
etc
我需要最快的方法来重组这个数组,使它最多有X个项目。如果我说X是3,那么得到的数组必须是:

Item 1 , Item 2
Item 3, Item 4
Item 5, Item 6
etc
或者,如果有7项,则为:

Item 1 , Item 2, Item 3,
Item 4, Item 5, 
Item 6, Item 7
最简单的方法是什么

我是从这个开始的,但似乎真的有一个更简单的方法:

foreach ($addressParts as $part)
{
    if (empty($part)) continue;
    if (empty($addressLines[$count]))  $addressLines[$count] = '';
    $addressLines[$count] .= $part;
    $count++;
    if ($count > 2) $count = 0;
}
此外,这也行不通,因为您最终会遇到以下情况:

item 1, item 4, item 7
item 2, item 5
item 3, item 6
。。。这是错误的。有什么想法吗

更新

如果我从以下几点开始:

Array
(
    [0] => item 1
    [1] => item 2
    [2] => item 3
    [3] => item 4
    [4] => item 5
    [5] => item 6
    [6] => item 7
)
最后,我想说:

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

有意义吗?

很抱歉,这应该是一条评论,但我不能发表评论,可能是因为我的声誉

当你有一个包含7个元素的数组时,你知道要将数组除以3还是必须找到除数

编辑1 像这样的

$formatedAddress = array();
$divisor = 3;
$key = 0;
$counter =1;
foreach ($addressParts as $part)
{
    if(!empty($part){
        $formattedAddress[$key] = $part;
        if($counter != $divisor){
           $counter++;
        }else{
            $counter = 1;
            $key++;
        }
    }

}
编辑2:

我发现了一些错误:

$formatedAddress = array();
$divisor = 3;
$key = 0;
$counter =1;
foreach ($addressParts as $part)
{
    if(!empty($part)){

    $formatedAddress[$key][] = $part;

    if($counter != $divisor){
       $counter++;
    }else{
        $counter = 1;
        $key++;
    }
    }
}

此函数根据您的示例将元素组合到新数组中。它处理任意数量的输入元素

function ReduceArray($input, $length) {
        $frac = $length / count($input);
        $frac = $frac + 0.0001;    // Offset for float calculations
        $index = 0.0;

        // Copy the elements, rolling over on $index
        $temp = array();
        foreach ($input as $part) {
                $i= floor($index);
                if (!isset($temp[$i])) {
                        $temp[$i] = array($part);
                } else {
                        $temp[$i][] = $part;
                }
                $index += $frac;
        }

        // Combine the sub arrays
        $output = array();
        foreach ($temp as $line) {
            $output[] = implode(', ', $line);
        }
        return $output;
}

$input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6', 'Item 7');
$output = ReduceArray($input, 3);
print_r($output);
输出

Array
(
    [0] => Item 1, Item 2, Item 3
    [1] => Item 4, Item 5
    [2] => Item 6, Item 7
)
根据给定输出编辑“固定”输出


编辑9个元素,请参见注释,最多测试12个元素。感谢sectus

对于每个组,计算第一个元素的偏移量和组长度,从输入数组复制该切片

function array_group_elements($array, $groups) {
  $result = array();
  $count = count($array);
  // minimum in each group
  $limit = floor($count / $groups);
  // balance, modulo
  $overhead = $count % $groups;
  // for each group
  for ($i = 0; $i < $groups; ++$i) {
    // group offset, add 1 for each group that got a balance element
    $offset = ($i * $limit) + ($i < $overhead ? $i : $overhead);
    // length, add 1 if it is a group with balance element
    $length = $limit + ($i < $overhead ? 1 : 0);
    // copy slice from original array
    $result[] = array_slice($array, $offset, $length);
  }
  return $result;
}

$input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6', 'Item 7');
$grouped = array_group_elements($input, 3);
var_dump(
  array_map(
    function($group) {
      return implode(', ', $group);
    },
    $grouped
  )
);

函数array_group_elements()在$groups(3次)上循环,而不是$array(7次)。

对于每个组,您需要计算适合每个段的最大项数,因此我使用了ceil();函数始终向上取整

输入:

Array
(
    [0] => item1
    [1] => item2
    [2] => item3
    [3] => item4
    [4] => item5
    [5] => item6
    [6] => item7
)
功能:

function segment_array($array, $segment = '3'){

    // Count the items
    $count = count($array);

    // Create an array item for each segment
    for($i=0;$i<$segment;$i++){
        $offset += ($count - ($count - $chunk));
        $chunk = ceil(($count - $offset)/($segment - $i));
        $set[$i] = array_slice($array, $offset, $chunk);
        $new_array[] = implode(', ', $set[$i]);
    }

    return($new_array);
}

$segmented = segment_array($array, '3');

试试这个:

function array_group($array, $X){
    $extra = count($array)%$X;
    $pre = array_splice($array, 0, $extra);
    $post = array_chunk($array, count($array)/$X);

    $post[0] = array_merge($pre, $post[0]);

    foreach ($post as &$key) {
        $key = implode(', ', $key);
    }
    return $post;
}

为任意数量的项目工作。在任意数量的列中进行更改,只需更改$x值

<?php
    $arr = array('Item 1','Item 2','Item 3','Item 4','Item 5','Item 6', 'Item 7');
    $x = 3;
    $newArr = array();
    $j = 0;
    foreach($arr as $key=>$val){
        if($j == $x){
            $j = 0;
        }
        $newArr[$j][] = $val;
        $j++;
    }
    foreach($newArr as $key=>$val){
        $tempVal = implode(',', $val);
        $newArr[$key] = $tempVal;
    }
    print_r($newArr);
    ?>

可以吗

$items = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7');
array_unshift($items, '');
$chunkitems = array_chunk($items, 2);
$newitems = array_map(function($v){return implode(', ', $v);}, $chunkitems);
$first = ltrim(implode(', ', array(array_shift($newitems), array_shift($newitems))),', ');
array_unshift($newitems, $first);
print_r($newitems);

我有两个不同方法的函数

function restructureArray1($array, $x)
    {
    $size = sizeof($array);
    if ($size < $x)
        return array_chunk($array, 1) + array_fill(0, $x, array());
        // chunk by 1 element and add missing empty elements

    $big_row_length = (int) ($size / $x) + 1;
    $big_rows_chunk = array_splice($array, 0, $size % $x * $big_row_length);
    // $big_rows_chunk contains items with big rows
    // $array now contains items with small rows
    return array_merge(array_chunk($big_rows_chunk, $big_row_length), array_chunk($array, $big_row_length - 1));
    // chunk and merge
    }

文档的相对链接:

p.S.最短解决方案


代码:

$input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6',  'Item 7');
$height = 3;
$commonWidth = floor(count($input) / $height);
$remaining = count($input) % $height;
$i = 0;
for ($j = 0; $j < $height; $j++) {
    $width = $commonWidth + (0 < $remaining--);
    $output[] = implode(', ', array_slice($input, $i, $width));
    $i += $width;
}
print_r($output);
我想知道问题描述是否不正确,并且所需的结果实际上是多维数组,而不是串联字符串的数组。在这种情况下,只需删除对内爆的调用:

$input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6',  'Item 7');
$height = 3;
$commonWidth = floor(count($input) / $height);
$remaining = count($input) % $height;
$i = 0;
for ($j = 0; $j < $height; $j++) {
    $width = $commonWidth + (0 < $remaining--);
    $output[] = array_slice($input, $i, $width);
    $i += $width;
}
print_r($output);

另一种算法,因为据我所知,问题是在适当的位置重新构造阵列,因此在不创建临时阵列的情况下可以实现最佳效率

<?php
function group_array(&$array, $parts = 3)
{
    $size = count($array);
    $length = floor($size / $parts);
    $remains = $size % $parts;
    $done = 0;
    for($i = 0; $i < $parts; ++$i)
    {
        $real_length = $length + ($i < $remains ? 1 : 0);
        $array[$i] = $array[$done];
        for($j = $done + 1; $j < min($done + $real_length, $size); ++$j)
            $array[$i] .= ', ' . $array[$j];
        $done += $real_length;
    }
    for($i = $size; $i >= $parts ; --$i)
        unset($array[$i]);
}

?>
测试用例#2:

<?php

$array = array("item 1", "item 2", "item 3", "item 4", "item 5", "item 6", "item 7");
group_array($array);

echo '<pre>' . print_r($array, true) . '</pre>';

?>
Array
(
    [0] => item 1, item 2, item 3, item 4
    [1] => item 5, item 6, item 7
    [2] => item 8, item 9, item 10
)

如果您对二维数组感到满意,并且如果最后一行可以比所有其他行小得多,您也会感到高兴,那么这里有一条直线:

Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7 ) 
Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8 ) 
Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8, Item 9 ) 
同样的事情,将每个片段合并为字符串:

$input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6', 'Item 7', 'Item 8',  'Item 9');
for ($size=7; $size<=count($input); $size++){
    $output = split_array2(array_slice($input, 0, $size), 3);
    print_r($output);
    echo '<br>';
}
三项测试的输出,包括7项、8项和9项:

function split_array3($orig_array, $parts = 1) {
    $count = count($orig_array);
    for ($i=0, $index=0; $i<$parts; $i++) {
        $size_of_sub = ceil(($count - $index) / ($parts-$i));
        $split[$i] = join(array_slice($orig_array, $index, $size_of_sub), ', ');
        $index += $size_of_sub;
    }
    return $split;
}
测试:

为了好玩,这里有一个使用递归的解决方案:

$a = range(1,7);

$len = count($a);
$res = array();
$max = 3;

$i = 0;
while ($i < $len) {
        // divide the remaining number of items by the maximum rows allowed
        // this gives the width for the current row
        $width = ceil(($len - $i) / $max);

        $res[] = join(', ', array_slice($a, $i, $width));

        // adjust pointer and reduce the maximum
        $i += $width;
        --$max;
}

print_r($res);

这似乎相当简洁:

<?php

function restructure($x,$slots)
{
  $result=array();
  $count=count($x);

  $least=(int)($count/$slots);
  $excess=$count-$least*$slots;

  for($i=0;$i<($least+1)*$excess;$i+=$least+1)
    array_push($result,implode(", ",array_slice($x,$i,$least+1)));
  for(;$i<$count;$i+=$least)
    array_push($result,implode(", ",array_slice($x,$i,$least)));
  return $result;
}

if (PHP_SAPI==='cli') {

  $x=array(
  "item 1",
  "item 2",
  "item 3",
  "item 4",
  "item 5",
  "item 6",
  "item 7",
  );

  print_r(restructure($x,3));

}

我提出了以下算法,可以在每一行中保持合适的项数;在每次迭代中,我们对剩余行数与剩余项数的划分进行四舍五入:

Array
(
    [0] => item 1, item 2, item 3
    [1] => item 4, item 5
    [2] => item 6, item 7
)
$a=范围(1,7);
$len=计数($a);
$res=array();
$max=3;
$i=0;
而($i<$len){
//将剩余的项目数除以允许的最大行数
//这将给出当前行的宽度
$width=ceil($len-$i)/$max);
$res[]=join(',',array_slice($a,$i,$width));
//调整指针并减小最大值
$i+=$width;
--$max;
}
印刷品(港币);;

可能还要一分钱。:-)


编辑:修复了指出的错误。请参见

伟大的问题+1。我已经创建了一个更具动态性的解决方案

  • 因为用户输入可以是1…n中的任意数字
  • 要描述或更改的成员的最小分组
  • 要描述或可随时更改的最大成员分组
  • 这将有助于其他人做出相应的改变:

    array (
      0 => 'Item 1, Item 2, Item 3',
      1 => 'Item 4, Item 5',
      2 => 'Item 6, Item 7',
    )
    
    
    

    这里有一个改编自(接近重复)

    代码:()


    数组\u chunk()可能吗?你只需要计算出最快执行的块大小,或者最小行数/复杂度?最小行数,最容易阅读。对不起,这对我来说没有任何意义。我看不出这些例子之间有什么区别。X是一个
    变量无符号整数
    传递的数组长度未知,必须按照什么规则依次分解?如果
    X=3
    分成三个子数组,其中包含等量的元素,如果
    X=7
    分成。。。我不明白。要么3和7是唯一可用的选项,而这两个例子是唯一可能的结果,要么我的大脑会烧坏derp@hanzo2001这两个例子(错的和好的)方向不同,错的先填充,好的先填充到右边,然后填充到下面。这里有趣的部分是在开始填充之前知道每行的长度,这与给定行号的换行有关,而不是给定的行长。现在,你可以假设它总是3。使其变化很容易。最近的编辑(我认为是最好的编辑)没有提供OP所需的输出数组。不仅一般结构不正确,OP还希望将值内爆为逗号分隔的值。证据:我们不能只使用其他部分吗?当我们只使用else部分时,输出保持不变。@vishal是的,然后您正在动态初始化数组。我喜欢在我的代码中避免这种情况,即使这会使我丧失“最少行”的资格;)很棒的东西stephan。保持9个项目的结果向上。幸运的是,这取决于偏移量,因为加法最终会溢出。
    $frac+=0.0001版本将失败。我想我们可以动态计算偏移量,比如
    $frac+=1/(count($input)*count($input))。但即使这样,10万件物品也将失败。这样的浮动很有趣。我刚刚意识到新的数组项需要尽可能地平衡。正在处理修订。已编辑:将for循环重新格式化为适当的di
    
    <?php
    function group_array(&$array, $parts = 3)
    {
        $size = count($array);
        $length = floor($size / $parts);
        $remains = $size % $parts;
        $done = 0;
        for($i = 0; $i < $parts; ++$i)
        {
            $real_length = $length + ($i < $remains ? 1 : 0);
            $array[$i] = $array[$done];
            for($j = $done + 1; $j < min($done + $real_length, $size); ++$j)
                $array[$i] .= ', ' . $array[$j];
            $done += $real_length;
        }
        for($i = $size; $i >= $parts ; --$i)
            unset($array[$i]);
    }
    
    ?>
    
    <?php
    
    $array = array("item 1", "item 2", "item 3", "item 4", "item 5", "item 6", "item 7");
    group_array($array);
    
    echo '<pre>' . print_r($array, true) . '</pre>';
    
    ?>
    
    Array
    (
        [0] => item 1, item 2, item 3
        [1] => item 4, item 5
        [2] => item 6, item 7
    )
    
    <?php
    
    $array = array("item 1", "item 2", "item 3", "item 4", "item 5", "item 6",
                   "item 7", "item 8", "item 9", "item 10");
    group_array($array);
    
    echo '<pre>' . print_r($array, true) . '</pre>';
    
    ?>
    
    Array
    (
        [0] => item 1, item 2, item 3, item 4
        [1] => item 5, item 6, item 7
        [2] => item 8, item 9, item 10
    )
    
    function split_array1($orig_array, $parts = 1) {
        return array_chunk($orig_array,ceil(count($orig_array)/$parts));    
    }
    
    function split_array2($orig_array, $parts = 1) {
        $split = array_chunk($orig_array,ceil(count($orig_array)/$parts));
        foreach ($split as &$row) $row = join($row, ', ');
        return $split;
    }
    
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7 ) 
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8 ) 
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8, Item 9 ) 
    
    $input = array('Item 1',  'Item 2',  'Item 3',  'Item 4',  'Item 5',  'Item 6', 'Item 7', 'Item 8',  'Item 9');
    for ($size=7; $size<=count($input); $size++){
        $output = split_array2(array_slice($input, 0, $size), 3);
        print_r($output);
        echo '<br>';
    }
    
    function split_array3($orig_array, $parts = 1) {
        $count = count($orig_array);
        for ($i=0, $index=0; $i<$parts; $i++) {
            $size_of_sub = ceil(($count - $index) / ($parts-$i));
            $split[$i] = join(array_slice($orig_array, $index, $size_of_sub), ', ');
            $index += $size_of_sub;
        }
        return $split;
    }
    
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5 [2] => Item 6, Item 7 ) 
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8 ) 
    Array ( [0] => Item 1, Item 2, Item 3 [1] => Item 4, Item 5, Item 6 [2] => Item 7, Item 8, Item 9 ) 
    
    function recursive_split_array($orig_array, $num_sub_arrays = 1) {
        $size_of_sub = ceil(count($orig_array) / $num_sub_arrays);
        $split[0] = join(array_slice($orig_array, 0, $size_of_sub),', ');
        $split = array_merge( $split,
                              split_array(array_slice($orig_array, $size_of_sub,
                                                      count($orig_array)-$size_of_sub),$num_sub_arrays - 1));
        return $split;
    }
    
    function ReduceArray($input,$length)
    {
        $count = count($input);
    
        // fill new array with new number of empty elements
        $newArray = array_fill(0,ceil($count/$length),"");
    
        for( $i = $count; $i > 0; $i--)
        {   
            // calculate index in new array to insert item
            $index = ceil($i / $length)-1;
    
            // we need a comma separator in this position if the array is empty
            $sep = ($newArray[$index] != "" ? "," : "");
    
            // insert into new array
            $newArray[$index] = array_pop($input) . $sep . $newArray[$index] ;
        }
    
        return $newArray;
    }
    
    $a = range(1,7);
    
    $len = count($a);
    $res = array();
    $max = 3;
    
    $i = 0;
    while ($i < $len) {
            // divide the remaining number of items by the maximum rows allowed
            // this gives the width for the current row
            $width = ceil(($len - $i) / $max);
    
            $res[] = join(', ', array_slice($a, $i, $width));
    
            // adjust pointer and reduce the maximum
            $i += $width;
            --$max;
    }
    
    print_r($res);
    
    <?php
    
    function restructure($x,$slots)
    {
      $result=array();
      $count=count($x);
    
      $least=(int)($count/$slots);
      $excess=$count-$least*$slots;
    
      for($i=0;$i<($least+1)*$excess;$i+=$least+1)
        array_push($result,implode(", ",array_slice($x,$i,$least+1)));
      for(;$i<$count;$i+=$least)
        array_push($result,implode(", ",array_slice($x,$i,$least)));
      return $result;
    }
    
    if (PHP_SAPI==='cli') {
    
      $x=array(
      "item 1",
      "item 2",
      "item 3",
      "item 4",
      "item 5",
      "item 6",
      "item 7",
      );
    
      print_r(restructure($x,3));
    
    }
    
    Array
    (
        [0] => item 1, item 2, item 3
        [1] => item 4, item 5
        [2] => item 6, item 7
    )
    
    <?
    
    //can n number of array items
    $items = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7');
    
    //counting of total number of items
    $total_items = count($items);
    
    //item to find in array
    $find_item = "item7";
    
    //first item found key number
    $key = array_search($find_item, $items);
    
    //Splitting into two
    
    //array with last found as item
    $temp_output1 = array_slice($items, 0, $key+1); 
    
    //items left
    $temp_output2 = array_slice($items, $key+1, $total_items); 
    
    //minimum number items to group
    $minimum_group=2;
    
    //Maximum Number of Grouped items
    $maximum_group=4;
    
    if ( $temp_output1 ) {
        //sending to group accordingly
        $output1 = do_slicing($temp_output1, $minimum_group, $maximum_group);
        print_r($output1);
    }
    
    if ( $temp_output2 ) {
        //sending to group accordingly
        $output2 = do_slicing($temp_output2, $minimum_group, $maximum_group);
        print_r($output2);
    }
    
    
    function do_slicing($temp_output = array(), $minimum_group, $maximum_group){
        $count = count($temp_output);
    
        //array is equal to min grouping
        if ( $count == $minimum_group){
            //Group them and return
            return by_grouping($temp_output, $minimum_group);
        }elseif ($count == $maximum_group){
            //if items equal to maximum then group them and return
            return by_grouping($temp_output, $maximum_group);
        }elseif ( ($count > $minimum_group) and ($count < $maximum_group)){
            return by_grouping($temp_output, count($temp_output));
        }elseif ($count == 1) {
            //if item is 1 print error or return from here
            return $temp_output;
        }else{
            //calculate the total number of groups available
            $item_slice_count = intval($count/$maximum_group);
    
            //Split them as grouped members
            $temp_slice = array_slice($temp_output, 0, 
                        ($item_slice_count*$maximum_group));
    
            //non group members
            $temp_slice2 = array_slice($temp_output, 
                        ($item_slice_count*$maximum_group), $count);
    
            //if there is no non group members
            if ( !$temp_slice2 ) {
                //combine them and return according to maximum grouping
                return by_grouping($temp_slice, $maximum_group);
            }else{
                if ( 
                    (count($temp_slice2) === $minimum_group) or 
                    (
                      (count($temp_slice2) > $minimum_group) and  
                      (count($temp_slice2) < 
                       $maximum_group)
                     ) 
                 ){
                    //if count of slice2 equal to minimum group then
                    $a=by_grouping($temp_slice, $maximum_group);
                    if ( 
                      (count($temp_slice2) > $minimum_group) and  
                      (count($temp_slice2) < 
                       $maximum_group)
                     ){
                        $b=by_grouping($temp_slice2, count($temp_slice2));
                    }else{
                        $b=by_grouping($temp_slice2, $minimum_group);
                    }
                    return array_merge($a, $b);
                }elseif( count($temp_slice2) < $minimum_group ) {
                    //if less then minimum group then
                    //if total count is divisible with minimum group
                    if ( ($count % $minimum_group ) == 0 ){
                        //return after clubbing according minimum group
                        return by_grouping($temp_output, $minimum_group);
                    }else{
                        //Where last group is not equal to minimum group
    
                        //Minimum more needed to complete last slice
                        $minimum_needed = $minimum_group - count($temp_slice2);
    
                        //slice1 become
                        $slice1 = array_slice($temp_output, 0, (
                                   ($item_slice_count-1)*$maximum_group));
    
                        //slice2 would be
                        $slice2 = array_slice($temp_output, (
                                          ($item_slice_count-1)*$maximum_group),
                                          ($maximum_group-$minimum_needed));
    
                        //slice 3 then
                        $slice3 = array_slice($temp_output, ( 
                                   (($item_slice_count-1)*$maximum_group) + 
                                  ($maximum_group-$minimum_needed)),
                                   $count);
    
                        //if slice2 is greater or equal to minimum group then
                        if ( $slice2>=$minimum_group) {
                            $a=by_grouping($slice1, $maximum_group);
                            $b=array_merge($a, by_grouping($slice2,
                                          count($slice2)));
                            $c=by_grouping($slice3, $minimum_group);
                            return array_merge($b, $c);
                        }else{
                            die("This Situation doesn't reached any time");
                        }
                    }
                }
            }
        }
    }
    
    //Grouping of members according to slices provided
    function by_grouping($temp_slice, $group){
        $return = array();
        $temp = array_chunk($temp_slice, $group);
        foreach($temp as $a){
            $return[] = implode(', ', $a);
        }
        return $return;
    }
    
    ?>
    
    function custom_chunk($array, $maxrows) {
        $result = [];
        $size = sizeof($array);
        $columns = ceil($size / $maxrows);
        $fullrows = $size - ($columns - 1) * $maxrows;
    
        for ($i = 0; $i < $maxrows; ++$i) {
            $result[] = implode(', ', array_splice($array, 0, ($i < $fullrows ? $columns : $columns - 1)));
        }
        return $result;
    }
    
    $data = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7'];
    
    var_export(custom_chunk($data, 3));
    
    array (
      0 => 'Item 1, Item 2, Item 3',
      1 => 'Item 4, Item 5',
      2 => 'Item 6, Item 7',
    )