So today I thought I’d go through a really good PHP function that I think a lot of you will find useful if you haven’t done already! The function is called array_chunk and what it does is quite simple. You feed into the function your array and a number. The number corresponds to what size chunks you want your array to be spit out at. So the function is used to split arrays.
You might be thinking why would I need something like this or why can’t I do this manually. Well the answer is simple – your array might not always be easy to access in code and it might even be dynamic. Splitting a posts loop in WordPress could be one example of this – maybe you want 4 posts in a slider but have no real way of achieving this. Using the array_chunk function (and a bit more problem solving) you can get to this.
I’m going to show you two ways of getting the same result. The first is probably what you will think of doing, or a solution you come up with during problem solving. The second way is the advanced array_chunk method. It is not critical whether or not you use this function, I think the biggest thing is being able to think in the right way to solve problems.For example, you could know every PHP function off by heart, but if you don’t know how to solve problems and put them in to practice – there’s no point learning them all.
Solution 1
So here is solution 1 – the problem solving method.
You can see that we have a variable of $array set to a string of numbers. We also have a variable of $i which we are using as our iterator and have set to 0. The $sum variable is how many items we want to be in each block – in this case it is 4. (see below)
<?php // problem solving function ?>
<?php $array = array("1","2","3","4","5","6","7","8","9"); ?>
<?php $i = 0; // start from 0 ?>
<?php $sum = 4; // how many per page ?>
<div>
<ul>
<?php foreach($array as $arr): // go through the array ?>
<?php if($i == $sum): $i = 0; // check for number echo'd is same as per page - reset number echo'd ?>
</ul>
</div>
<div>
<ul>
<?php endif; // we close div / ul within this if ?>
<li><?php echo $arr; ?> - <?php echo $i; ?></li>
<?php $i++; // increase the iterator ?>
<?php endforeach; ?>
</ul>
</div>
Now things get a little more complicated. We have a FOREACH statement which will run through all of our array items and echo out the corresponding code. We also have a statement in there which increases our iterator value each time it is looped through.
So we need it to close our UL for every 4 LI’s that are looped through. That’s why we have an IF statement just below the FOREACH statement. It basically reads – if our iterator is equal to our sum, then do the following. This is where we end the UL and DIV tags, and then re-open them because we are also resetting our iterator back to 0.
This is kind of the solution that you would think of before trying to compress the code down and looking for a function that does the same thing.
Solution 2
Here is the same problem, but using the array_chunk function to help us. Again we have a variable set to our array full of items. Then we are setting another variable to the output of our array_chunk function. We have fed in our array variable and also the number we need it to be split into. From there we can use a series of FOREACH statements to loop through and output what we want. (see below)
<?php // easy method ?> <?php $values = array_chunk($array, 4); // inbuilt function (PHP 4 >= 4.2.0, PHP 5) ?> <?php foreach($values as $val): ?> <div> <ul> <?php foreach($val as $v): ?> <li><?php echo $v; ?></li> <?php endforeach; ?> </ul> </div> <?php endforeach; ?>
The first FOREACH statement we are using loops through each split array, opening and closing DIV’s and UL’s. The second FOREACH statement echoes out the LI’s within our split arrays.
You can see, the same problem can be solved in many ways, but the key is to be able to solve your problem – not knowing every single PHP function that exists. Like I said, if you can’t solve the problem, why do you need to learn the function.
I hope this weeks web design tutorial has helped, any questions or comments please leave below.



great and easy php splitting tutorial
I want to say thank you a whole lot for that job you have made in writing this posting. I am hoping the same most effective job from you in the future as well.