Home » PHP

PHP array_chunk() Function with Example

PHP array_chunk() Function: Here, we are going to learn about the array_chunk() function with example in PHP.
Submitted by IncludeHelp, on February 07, 2019

PHP array_chunk() Function

array_chunk() function is an array function, it is used to split a given array in number of array (chunks of arrays).

Syntax:

    array_chunk(array_name, size, [preserve_keys]) : array

Here,

  • array_name is the main array, which we have to convert into array chunks.
  • size is the number of arrays to be converted.
  • preserve_keys is an optional parameter and its default value is false. If it is true keys will be preserved and if it is “false” the keys of the arrays will be re-indexed in sub arrays.

It returns a multidimensional array.

Examples:

    Input:
    $arr = array("New Delhi", "Mumbai", "Chennai", "Pune", "Gwalior");

    Output:
    Array
    (
        [0] => Array
            (
                [0] => New Delhi
                [1] => Mumbai
                [2] => Chennai
            )

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

    )

PHP code 1:

<?php
	$arr = array("New Delhi", "Mumbai", "Chennai", "Pune", "Gwalior");
	print ("Original array is...\n");
	print_r ($arr);
	
	$arr1 = array_chunk($arr, 3);
	print ("array (size: 3) is...\n");
	print_r ($arr1);	
?>

Output 1

Original array is...
Array
(
    [0] => New Delhi
    [1] => Mumbai
    [2] => Chennai
    [3] => Pune
    [4] => Gwalior
)
array (size: 2) is...
Array
(
    [0] => Array
        (
            [0] => New Delhi
            [1] => Mumbai
            [2] => Chennai
        )

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

)

PHP Code 2: with preserved – "true"

<?php
	$arr = array("New Delhi", "Mumbai", "Chennai", "Pune", "Gwalior");
	print ("Original array is...\n");
	print_r ($arr);
	
	$arr1 = array_chunk($arr, 3, true);
	print ("array (size: 2) is...\n");
	print_r ($arr1);	
?>

Output 2

Original array is...
Array
(
    [0] => New Delhi
    [1] => Mumbai
    [2] => Chennai
    [3] => Pune
    [4] => Gwalior
)
array (size: 2) is...
Array
(
    [0] => Array
        (
            [0] => New Delhi
            [1] => Mumbai
            [2] => Chennai
        )

    [1] => Array
        (
            [3] => Pune
            [4] => Gwalior
        )

)

See the output 1 & Output 2, in first output sub arrays are re-indexed while in output 2 arrays are not re-indexed.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.