lets say there is an array like this [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
. How can add every 7 values into a new array that looks like this [[0,1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16]
Asked
Active
Viewed 44 times
1 Answers
2
If the arrays will be equally sized (your example has 7 values in the first array and six in the second - not sure if it's a typo or not) then you can do this:
$array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
$chunkedArrays = array_chunk($array, 7);
print_r($chunkedArrays);
array_chunk
documentation here

martincarlin87
- 10,848
- 24
- 98
- 145
-
1You've been faster than me, kudos to you :) – Cid Dec 27 '21 at 16:59
-
1ah yes it was a typo, supposed to be 7 values in the first two and then the remaining in the last one, thanks! – Stanleyy Dec 27 '21 at 17:01
-
1@Cid ha - sorry, it's not often I am the fastest when answering a question on SO! – martincarlin87 Dec 27 '21 at 17:02