php将一维数组转换为每3个连续值组成的二维数组
本文实例讲述了php实现将一维数组转换为每3个连续值组成的二维数组。分享给大家供大家参考,具体如下:
- <?php
- $aaa = array('aa','bb','cc','dd','ee','ff','gg','hh','ii');
- for($i=0;$i<3;$i++)
- //phpfensi.com
- {
- $bbb[] = array_slice($aaa, $i * 3 ,3);
- }
- print_r($bbb);
- ?>
运行结果如下:
- 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
- )
- )
关键代码:
$bbb[] = array_slice($aaa, $i * 3 ,3);
//3为3个一组,如果是2为2个一组