PHP array_chunk() Function

PHP Tutorial

The PHP array_chunk() Function is used to split array values into chunks of arrays. So how many array chunks it will create that depends on your given array chunk size.


Syntax

array_chunk(array, size, boolean_value);
  1. array – This parameter takes an array.
  2. size – The size determines the maximum value of each array chunk.
  3. boolean_value – This is an optional parameter and it takes a boolean value (true or false). This parameter is used when you work with an associative array. The Default value is false.
    • true – It Preserves the associative array keys
    • false – It converts the associative array chunks into numeric array chunks.

How does the PHP array_chunk() Function work?

See the following Image to understand that how does the PHP array_chunk() Function work.

PHP array_chunk() Function

Example with a Numeric Array

<?php
$data = array(
    "John", "Mark",
    "Adam", "Joe", 
    "Bruce", "Max", "Sam"
);

$new_data = array_chunk($data,2);

echo "<pre>";
var_dump($new_data);
echo "</pre>";
?>
Browser Output
array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(4) "John"
    [1]=>
    string(4) "Mark"
  }
  [1]=>
  array(2) {
    [0]=>
    string(4) "Adam"
    [1]=>
    string(3) "Joe"
  }
  [2]=>
  array(2) {
    [0]=>
    string(5) "Bruce"
    [1]=>
    string(3) "Max"
  }
  [3]=>
  array(1) {
    [0]=>
    string(3) "Sam"
  }
}

Example with an Associative Array

<?php
$data = array(
    "id" => 1,
    "first_name" => "John",
    "last_name" => "Doe",
    "age" => 21,
    "email" => "[email protected]",
    "phone" => "1-770-736-8031 x56442",
    "website" => "hildegard.org"
);

$new_data = array_chunk($data,2);

echo "<pre>";
var_dump($new_data);
echo "</pre>";
?>
Browser Output
array(4) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    string(4) "John"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "Doe"
    [1]=>
    int(21)
  }
  [2]=>
  array(2) {
    [0]=>
    string(14) "[email protected]"
    [1]=>
    string(21) "1-770-736-8031 x56442"
  }
  [3]=>
  array(1) {
    [0]=>
    string(13) "hildegard.org"
  }
}

Preserving the associative array keys

If you want to preserve associative array keys then you need to add the optional parameter and the parameter value is true.

<?php
$data = array(
    "id" => 1,
    "first_name" => "John",
    "last_name" => "Doe",
    "age" => 21,
    "email" => "[email protected]",
    "phone" => "1-770-736-8031 x56442",
    "website" => "hildegard.org"
);

$new_data = array_chunk($data, 2, true);

echo "<pre>";
var_dump($new_data);
echo "</pre>";
?>
Browser Output
array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["first_name"]=>
    string(4) "John"
  }
  [1]=>
  array(2) {
    ["last_name"]=>
    string(3) "Doe"
    ["age"]=>
    int(21)
  }
  [2]=>
  array(2) {
    ["email"]=>
    string(14) "[email protected]"
    ["phone"]=>
    string(21) "1-770-736-8031 x56442"
  }
  [3]=>
  array(1) {
    ["website"]=>
    string(13) "hildegard.org"
  }
}

Quick Notes:

How to split array values into array chunks?

The PHP array_chunk() Function is used to split array values into chunks of arrays. So how many array chunks it will create that depends on your given array chunk size.

You May Also Like

Leave a Comment

Your email address will not be published. Required fields are marked *