Php-Arrays function-chunk-splits an array into multiple Arrays

Source: Internet
Author: User

Chunk () function
[Function]
This function splits an array into multiple arrays,
The number of units in each array is determined by the size.
The number of units in the last array may be smaller.
The obtained array is a unit in a multi-dimensional array, and its index starts from scratch.
Scope of use]
> = 4.2.0, php5
[Use]
Array array_chunk (array input, int size [, bool preserve_keys])
Input/required/split Array
Size/required/number of units in the array
Preserve_keys/optional/True reserve the original key name of the array,
The value of the False key is a numeric index starting from scratch. The default value is False.
[Example]
[Php]
<Span style = "font-size: 16px;"> <? 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
)
 
)
*/</Span>

 

 

Taken from zuodefeng's note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.