Splitting an array into columns

  • 1 Min. Read.

Let’s assume we have the an array with the following values:

How would we divide these values over 3 columns using PHP

The following function takes an array, the number of columns you want that array to be split in and a boolean that specifies if you want to preserve the keys in your array. After some calculations, you’ll get a new multidimensional array, containing your 3 columns.

This will output the following new array using our example:

And we can now loop over this new array with a nested for loop, and output some HTML to position the columns next to each other.

 

Was this article helpful?

Comments 0
There are no comments at the moment.
A concept by Wonderlus

Warning: Parameter 1 to W3_Plugin_TotalCache::ob_callback() expected to be a reference, value given in /mnt/home_bingo/codecaptain/codecaptain.io/public/wp-includes/functions.php on line 3583