Home  >  Article  >  php教程  >  php-Arrays函数-chunk-将一个数组分割成多个数组

php-Arrays函数-chunk-将一个数组分割成多个数组

WBOY
WBOYOriginal
2016-06-13 10:49:41906browse

chunk() 函数
【功能】
         该函数将将一个数组分割成多个数组,
         其中每个数组的单元数目有size决定
         最后一个数组的单元数目可能会少几个
         得到的数组是一个多维数组中的单元,其索引从零开始
【使用范围】
         >=4.2.0、php5
【使用】
         array array_chunk(array input ,int size [,bool preserve_keys] )
         input/必需/被分割的数组
         size/必需/分割数来数组的单元数目
         preserve_keys/可选/True保留数组原来的键名,
                  False键名为从零开始的数字索引,默认为False
【示例】
[php]
        $arr = array( "key1" => "val1", "key2" => "val2",  
                      "key3" => "val3", "key4" => "val4"); 
        print_r( array_chunk( $arr, 2 ) ); 
        print_r( array_chunk( $arr, 2, True ) ); 
        print_r( array_chunk( $arr, 3 ) ); 
/*
Array
(
    [0] => Array
        (
            [0] => val1
            [1] => val2
        )
 
    [1] => Array
        (
            [0] => val3
            [1] => val4
        )
 
)
Array
(
    [0] => Array
        (
            [key1] => val1
            [key2] => val2
        )
 
    [1] => Array
        (
            [key3] => val3
            [key4] => val4
        )
 
)
Array
(
    [0] => Array
        (
            [0] => val1
            [1] => val2
            [2] => val3
        )
 
    [1] => Array
        (
            [0] => val4
        )
 
)
*/
 

 

 

摘自 zuodefeng的笔记

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