Array chunk example with PHP


Posted by : Darko Borojevic - contact me

Array chunk algorithm with PHP

This example can show You how to think about dividing bigger Arrays into smaller units, for the purpose of let's say, building a custom pagination. Here, we have built a custom function, fairly simple one, that takes an array of numbers as an input (and yes, I'm using PHP-7 type hints here), and constructs rows based on the second parameter which is a number of columns wanted.

Code example

array chunk with php
This type of algorithms can be useful in building helper functions for constructing widgets, paginations, tag clouds etc., but the important lesson is to use this type of thinking in different code repeating situations where different array php functions can be utilized along with loops to automate well formated data rendering.

PHP array_chunk() function usage for similar tasks

We can also use PHP's array_chunk() function to split an array in chunks of certain size, depending upon the parameters that are passed to the function. First parameter has to be an array, and the second parameter is an integer that determines how many chunks will be applied. This function returns a multidimensional numerically indexed array, with each dimension containing size elements starting at 0. The last chunk may contain less elements than desired size of the chunk. If the second parameter is equal or larger than the original array size, the function returns the original array. If size is less than 1, E_WARNING will be thrown. The $preserve_keys parameter takes Boolean value, and if this argument is set to TRUE then the keys are preserved, otherwise the chunk is reindexed starting from 0.

Array example

chunk array php example 1
Now we apply array_chunk() with true flag as the third parameter:

Array_chunk() with true flag parameter

chunk array php example function
Now we get $food delivered as two sub arrays, but with the same original keys preserved, as seen below. This functionality can be useful in real life scenarios, where for example, we have a large array, but now we would like to chunk that array but in a way that we group certain parts of that array together, and preserving the original keys in the process.

php array chunk - result 1

chunk array php result 1
With the third parameter set to false (which is the default if we leave it empty), we get the same result but we do not have the original keys preserved. This can be useful in other situations where it may be convenient for us to have numeric keys, or simply have new keys instead of the old ones. So this result will now look like this:

php array chunk - result 2

chunk array php result 2
Chunking arrays is often used in situations where we need to group elements of multidimensional arrays in a different manner than we originally planned. Using functions similar to these, we can shorten the time volume that would be used if we would manually refactor large arrays. Luckily, PHP offers a wide variety of functions like array_chunk() that can help us with tasks like that. To utilize multidimensional arrays in our server-side code, is therefore much easier and it is not prone to give us a big headache.



Do you have a comment?

Your E-mail


Your Message