Free cookie consent management tool by TermsFeed Generator PHP Array Chunk | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Chunk

PHP Array Chunk

Last Updated on Feb 14, 2023

Introduction

Sometimes you have to deal with a very large array of data. It would be much better to split the array in different chunks and work on each chunk one at a time.

For example if you have an array with 1000 elements you can split it in arrays of 10 or 100.

Array chunk is going to help you do exactly that.

Array Chunk

array_chunk($array,$length,$preserveKeys)

It takes 3 arguments

array is the original array

Length is the length of each chunk

PreserveKey is false by default. It reindexes your array with numerical values. But if you have an associative array and you want to keep the keys as is, make sure to set this to true

Let’s see an example

I have an array of 5 people (it’s not big I know but imagine it’s 500) and I want to split it into arrays with length of 2. And I want my keys to stay the same. So let’s see the code

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver'
];
$chunks = array_chunk($people,2,true);
//$chunks = array_chunk($people,2,true);
// Array (
// [0] => Array (
//      [php]       => amir
//      [feedhive]  => simon
//      )
// [1] => Array (
//      [twitter]   => pratham
//      [docker]    => francesco
//      )
// [2] => Array (
//      [web3]      => oliver
//      )
// )

As you can see it has returned an array of arrays. Each array is in length 2 except the last one which is in length 1 because there were no other items.

Now you can access each array and work on them. array_chunk is a great function isn’t it?

Conclusion

Now you know about array chunck function in PHP.

I recommend you to open a PHP files and try to define an array. then try to search the values inside it and see the result.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array chunk function in PHP
  • split array into smaller chunks

 

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...