Home > Article > Backend Development > php-Arrays function-chunk-split an array into multiple arrays_PHP tutorial
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] =>
)
)
*/
http://www.bkjia.com/PHPjc/478197.html
www.bkjia.com