Split an Array into N Arrays - PHP

Splitting of array into multiple arrays

If you need such a variable array length of chunks then you can create your own functionality using simple foreach and array_splice like as

$array=[1,2,3,4,5,6,7,8,9,10];

$arr = [3,3,4];
$result_arr = [];
foreach($arr as $k => $v){
$result_arr[$k] = array_splice($array,0,$v);
}
print_R($result_arr);

Output:

Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)

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

[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 10
)

)

Split an Array into N Arrays - PHP

This simple function would work for you:

Usage

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

Output

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
)

)

Update The above might not be useful for most cases ... here is another type of chunk

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

Output

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
)

)

This would make sure the group at no time is more that 5 elements where the other one has no limitation

Function Used

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;
}

Split an array into multiple arrays depending on number of similar adjacent values

I don't recommend the use of dynamic number of resulting vars like you asked in your question. It's better to have the result inside an array. This way you can cycle through the array indexes, instead of guessing how many $input_array_(number) your code generated.

$input_array = array('45', '21', '45', '45', '45', '29', '35', '35', '21');

$result = [];
$index = 0;
$processedElement = null;
foreach ($input_array as $currentElement) {
if ($processedElement != $currentElement) {
$index++;
}
$result[$index][] = $currentElement;
$processedElement = $currentElement;
}

Printing $result will give you:

Array
(
[0] => Array
(
[0] => 45
)
[1] => Array
(
[0] => 21
)
[2] => Array
(
[0] => 45
[1] => 45
[2] => 45
)
[3] => Array
(
[0] => 29
)
[4] => Array
(
[0] => 35
[1] => 35
)
[5] => Array
(
[0] => 21
)
)

split an array into multiple part based on key pattern

If I understand correctly you can do it like the following, but it is an XY problem, why are you not structuring your arrays properly first?

$normalised = [];
foreach ($array as $key => $value) {
list($k, $a) = explode('-', $key, 2);
$normalised[$k][$a] = $value;
}

Split array in multiple arrays populate by order in PHP

You could iterate your values and fill array using modulo :

$input_array = array('a', 'b', 'c', 'd', 'e','f','g','h');
$arrs = [];
$key = 0;
foreach ($input_array as $val) {
$arrs[$key++ % 4][] = $val;
}

// $arrs contains what you want.

// Then you could extract them in separate arrays.
list($a1, $a2, $a3, $a4) = $arrs;
var_dump($a1, $a2, $a3, $a4);

Outputs :

array(2) {
[0]=> string(1) "a"
[1]=> string(1) "e"
}
array(2) {
[0]=> string(1) "b"
[1]=> string(1) "f"
}
array(2) {
[0]=> string(1) "c"
[1]=> string(1) "g"
}
array(2) {
[0]=> string(1) "d"
[1]=> string(1) "h"
}

php split array into smaller even arrays

EDIT: There's array_chunk, which does just that.

Well, I didn't feel like debugging, so I wrote a version with array_reduce:

$pergroup = 2;
$redfunc = function ($partial, $elem) use ($pergroup) {
$groupCount = count($partial);
if ($groupCount == 0 || count(end($partial)) == $pergroup)
$partial[] = array($elem);
else
$partial[$groupCount-1][] = $elem;

return $partial;
};

$arr = array(1,2,3,4,5);

print_r(array_reduce($arr, $redfunc, array()));

gives

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

[1] => Array
(
[0] => 3
[1] => 4
)

[2] => Array
(
[0] => 5
)

)

Split Multi-dimensional Array Into Multiple Arrays

As per @devpro comment (write it here to have official answer):

You can use array-chunk which chunks an array into arrays with size elements. The last chunk may contain less than size elements.

Use it in your case as:

$chuncks = array_chunk($array, $limit));

Split array into equal parts order by id using PHP

Sounds like you wanted something like this:

$array = [
[
'id' => 121,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 139,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 1456,
'owner' => 'xv',
'name' => 'bjs',
],
[
'id' => 1896,
'owner' => 'xb',
'name' => 'bjs',
],
[
'id' => 1963,
'owner' => 'xb',
'name' => 'bjs',
]
];

// custom function to compare which ID is greater
function customCompare($a, $b)
{
if ($a['id'] === $b['id']) {
return 0;
}

return ($a['id'] < $b['id']) ? -1 : 1;
}

// sort the whole array
usort($array, "customCompare");

// print the whole array as pairs,
// if there is an unpair number
// the last one will be a single member
print_r(array_chunk($array, 2));


Related Topics



Leave a reply



Submit