Home  >  Article  >  php教程  >  array_chunk:将一个数组分解成包含多个一维数组的二维数组

array_chunk:将一个数组分解成包含多个一维数组的二维数组

PHP中文网
PHP中文网Original
2016-05-23 16:40:081303browse

array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )
input:需要操作的数组
size:每个数组的单元数目
preserve_keys:设为 TRUE,可以使 PHP 保留输入数组中原来的键名。 FALSE,每个结果数组将用从零开始的新数字索引。默认值是 FALSE。

1.php代码

$arr1 = ['a','b','c','d','e','f','g'];
$arr = array_chunk($arr1,3,true);
 
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($arr);
 
result:
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )
 
    [1] => Array
        (
            [3] => d
            [4] => e
            [5] => f
        )
 
    [2] => Array
        (
            [6] => g
        )
 
)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn