Array chunk example with PHP

Array chunk example with PHP

Array chunk algorithm example 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 that we need.

Starter code

array chunk with php

This type of algorithms can be useful in building helper functions for constructing widgets, pagination, 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 formatted data rendering.

PHP array_chunk() function scenario for similar tasks

We can also use PHP array_chunk() function to split an array into 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 $length 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 a boolean value, and if this argument is set to TRUE then the keys are preserved, otherwise the chunk is re-indexed 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 the array together, and preserving the original keys in the process.

PHP array chunk - result 1

chunk array php result 1

When the third parameter is 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 giving us a big developer headache. Happy coding!


Previous Post TypeScript - the missing link of JavaScript
Next Post jQuery + Bootstrap 4 contact form validation
Comments (0)



Leave a Reply

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