Usage of array_chunk in PHP

Source: Internet
Author: User

Transferred from: http://cn2.php.net/manual/zh/function.array-chunk.php

(PHP 4 >= 4.2.0, PHP 5)

array_chunk- splits an array into multiple

Descriptionarray array_chunk ( array $input , int $size [, bool $preserve_keys = false])

Splits an array into multiple arrays, each of which is determined by the number of cells in each array size . The number of cells in the last array may be less than size one.

Parameters

input

Array to operate on

size

Number of cells per array

preserve_keys

Set to TRUE allow PHP to keep the original key name in the input array. If you specify FALSE , then each result array will be indexed with a new zero-based number. The default value is FALSE .

return value

The resulting array is a cell in a multidimensional array whose index starts at zero and each dimension contains size elements.

Error/exception

If size it is less than 1, an error is thrown E_WARNING and returned NULL .

Example

Example #1 array_chunk () example

<?php
$input_array = array(‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘);
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The above routines will output:

Array (    [0] = = Array        (            [0] = a            [1] = b        )    [1] = = Array        (            [0] = = C            [1 ] + D        )    [2] = =        array            ([0] = e        )) array ([    0] = = Array        (            [0] = a            [1] = b        )    [1] = = Array        (            [2] = C            [3] + D        )    [2] = = Array        (            [4] = e        ))

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.