Home  >  Article  >  Backend Development  >  php-Arrays function-chunk-split an array into multiple arrays_PHP tutorial

php-Arrays function-chunk-split an array into multiple arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:291594browse

chunk() function
【Function】
This function will split an array into multiple arrays,
​​​​The number of cells in each array is determined by size
The number of cells in the last array may be a few less
The resulting array is a cell in a multidimensional array, and its index starts from zero
【Scope of use】
            >=4.2.0, php5
【Use】
array array_chunk(array input ,int size [,bool preserve_keys] )
​​​​​ input/required/split array
size/required/split number to determine the number of cells in the array
Preserve_keys/optional/True retains the original key names of the array,
False key name is a digital index from scratch, and defaults to False
【Example】
[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] => [1] = & gt; Val2
)

[1] => Array
(
                                                   [0] => ​​​​​[1] => )

)
Array
(
[0] => Array
(
[key1] => val1
[key2] => val2
)

[1] => Array
(
[key3] => val3
[key4] => val4
)

)
Array
(
[0] => Array
(
​​​​​[0] => [1] = & gt; Val2
[2] = & gt; Val3
)

[1] => Array
(
                                                   [0] => )

)
*/





Excerpted from zuodefeng’s notes

http://www.bkjia.com/PHPjc/478197.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/478197.htmlTechArticlechunk() function [Function] This function will split an array into multiple arrays, where each array The number of cells depends on size, which determines how many cells the last array may have...
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