Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split array into a specific number of chunks

Tags:

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.

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?

$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.

like image 841
SnitramSD Avatar asked Mar 30 '13 20:03

SnitramSD


People also ask

How do you divide an array into multiple parts?

Splitting the Array Into Even Chunks Using slice() Method The easiest way to extract a chunk of an array, or rather, to slice it up, is the slice() method: slice(start, end) - Returns a part of the invoked array, between the start and end indices.

How do you divide an array into 4 parts?

Given an array of n non-negative integers. Choose three indices i.e. (0 <= index_1 <= index_ 2<= index_3 <= n) from the array to make four subsets such that the term sum(0, index_1) – sum(index_1, index_2) + sum(index_2, index_3) – sum(index_3, n) is maximum possible.

Which function is used to divide an array into smaller evenly sized arrays?

concat(split(array. slice(size), cols-1)) .


2 Answers

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; } 
like image 172
Baba Avatar answered Sep 27 '22 21:09

Baba


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/

like image 39
Andrei Surdu Avatar answered Sep 27 '22 20:09

Andrei Surdu