怎样把一维数组的每四位作为一个单元转成二维数组?

代码:

$o = range  (1,50);
print_r ($o);
 
$new =array ();
for ($i=0; $i<count($o); $i=$i+4) {
    $new[]  = array_slice($o,  $i,  4);
}
print_r ($new);
exit;

结果 :

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
 
    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )
 
    [2] => Array
        (
            [0] => 9
            [1] => 10
        )
 
)

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

*
请输入图片中的字符以验证你并非垃圾机器人. 点击图片收听验证码的语音版.
点击这里收听此验证码的语音版本