0

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]

Stanleyy
  • 302
  • 3
  • 12

1 Answers1

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