Array input:
$input_array = array('a', 'b', 'c', 'd', 'e','f','g','h');
Desired output (4 arrays for example):
Array(
'a',
'e'
)
Array(
'b',
'f'
)
Array(
'c',
'g'
)
Array(
'd',
'h'
)
As you can see, it's different output than array_chunk. I want to fill in order the different arrays, one by one, when reach the last array go back to the first array and repeat until no more values.
I've tried so many possibilities but can't find the good one.
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"
}