Array chunking is a great way to paginate large arrays and as I found out recently, makes it very easy to map a large array from Redis (by way of
mget and/or pipelining) back to the original data. Chunking an array is the process of taking an array and splitting it into chunks. This can be done easily with the
Let’s say you have large array of 100 records and you want to break it into groups of 10, to do so you would feed it to
array_chunk() with the value of 10:
From time to time I will keep a full data set in cache and use this method to be able to break the data out into pages. Once it’s chunked, you can request it as if it were a page of data:
We have to decrease the requested page number by one because the chunked array will start at 0.
The other use case that I’ve utilized the chunking with is when you use
mget to retrieve multiple keys from Redis or when utilizing pipelining with Redis both of which will result in one flat array. Let’s say we have an array of users and want to loop through and pull 3 keys each out of Redis for each user. You could loop through the users and run
mget for each user, but this is obviously inefficient and you should attempt to pull all of the keys at once or better still, pipeline it to reduce the network overhead. I’ll show both examples as they can both benefit from array chunking.
mget to pull multiple keys for the users:
The Redis command
mget returns a flat array of all of the values (null if they aren’t present, maintaining a full structure), once you chunk the data you could use
array_combine() to apply key names to the chucked array instead of using the integer keys as this example shows. I’ll show you that in the next example ;)
The next example works around Redis pipelining, which would work if you are mixing Redis commands like
zcard and such. Using the same hypothetical user IDs array from earlier:
Similar workflow to the previous example but actually a bit simpler with the inclusion of the
array_combine() and could probably be further simplified by using the
$user_id value as the key on the
$users array instead of including it in as an array element. Thinking it through further, because the Redis information is returned in the other that you request it, you could use the
$user_ids array and the chunks return from Redis with
array_combine to reassemble the data in a single line:
So that’s how you can use
array_chunk() along with some of my own use cases. If you already use
array_chunk() or can think of another use case that I didn’t touch on, comment below!