febri it febri it - 1 month ago 7
PHP Question

How to cut array by total column and make new array for the other

I have array example :

array(21) {[0]=>1 [1]=>"AAA" [2]=>"BBB" [3]=>"CCC" [4]=>"DDDD" [5]=>"EEE" [6]=>"FFF" [7]=>2 [8]=>"GGG" [9]=>"KKK" [10]=>"LLL" [11]=>"MMM" [12]=>"NNN" [13]=>"OOO" [14]=>3 [15]=>"PPP" [16]=>"QQQ" [17]=>"RRR" [18]=>"SSS" [19]=>"TTT" [20]=>"UUU" }


and, cut by 7 column and create new array, example :

array(21) {[0]=>1 [1]=>"AAA" [2]=>"BBB" [3]=>"CCC" [4]=>"DDDD" [5]=>"EEE" [6]=>"FFF"}
array(21) {[0]=>2 [1]=>"GGG" [2]=>"KKK" [3]=>"LLL" [4]=>"MMM" [5]=>"NNN" [6]=>"OOO"}
array(21) {[0]=>3 [1]=>"PPP" [2]=>"QQQ" [3]=>"RRR" [4]=>"SSS" [5]=>"TTT" [6]=>"UUU"}


So,How to cut array by total column and make new array for the other?

Thankyou

Answer

With your data expecting every 7th item to be the end of a set, you can make use of array_chunk.

$output_array = array_chunk( $input_array, 7);