Simple example of grouping PHP arrays

Source: Internet
Author: User
Tags arrays
The code is as follows Copy Code

<?php
$teams =array (1,2,3,4,5,6,7,8,9);
$teams =array_chunk ($teams, 2);
Print_r ($teams);
/*

Array

(


[0] => Array


(


[0] => 1


[1] => 2


)



[1] => Array


(


[0] => 3


[1] => 4


)



[2] => Array


(


[0] => 5


[1] => 6


)



[3] => Array


(


[0] => 7


[1] => 8


)



[4] => Array


(


[0] => 9


)


)

*/

?>

The array_chunk () function splits an array into a new array block.
The number of cells in each array is determined by the size parameter. The number of cells in the last array may be few.
An optional parameter, Preserve_key, is a Boolean value that specifies whether the elements of the new array have the same key (for associative arrays) as the original array, or a new number key (for an indexed array) starting with 0. The default is to assign a new key.

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.