Home >Backend Development >PHP Tutorial >PHP converts a one-dimensional array into a two-dimensional array consisting of every 3 consecutive values
The example of this article describes the PHP implementation of converting a one-dimensional array into a two-dimensional array composed of three consecutive values. Share it with everyone for your reference, the details are as follows:
<?php $aaa = array('aa','bb','cc','dd','ee','ff','gg','hh','ii'); for($i=0;$i<3;$i++) { $bbb[] = array_slice($aaa, $i * 3 ,3); } print_r($bbb); ?>
The running results are as follows:
Array ( [0] => Array ( [0] => aa [1] => bb [2] => cc ) [1] => Array ( [0] => dd [1] => ee [2] => ff ) [2] => Array ( [0] => gg [1] => hh [2] => ii ) )
Key code:
$bbb[] = array_slice($aaa, $i * 3 ,3); //3为3个一组,如果是2为2个一组
I hope this article will be helpful to everyone in PHP programming.
For more articles related to PHP converting a one-dimensional array into a two-dimensional array composed of three consecutive values, please pay attention to the PHP Chinese website!