

array_chunk in PHP
source link: https://stitcher.io/blog/array-chunk-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

array_chunk in PHP
Heads up!
This is a guest post, written by Dan Englishby. If you're interested in writing guest posts yourselves, you can find all information on how to over here.PHP has a lot of undiscovered native functions for many devs. Usually, we don't discover these functions until there is a true need for them. array_chunk
is certainly one of these functions for me personally. In this article, we will discover what array_chunk
is, what it does, and see it used in action.
# array_chunk
and how it works
array_chunk
is a pretty nifty name for slicing an array into chunks. This is essentially what the function does, it reads an array, and splits it into chunks.
Let me explain this in developer terms: we have an array, [1, 2, 3, 4, 5, 6, 7, 8]
, we use array_chunk
on the array, specifying we want chunks of 2 items. The output would look similar to below.
$input = [1, 2, 3, 4, 5, 6, 7, 8];
array_chunk($input, 2);
[
0 => [1, 2],
1 => [3, 4],
2 => [5, 6],
3 => [7, 8],
]
Pretty cool eh? With this function, you can quickly see how it can be utilized for statistical purposes, especially with segment averaging.
This function accepts 3 parameters as follows:
- The
array
- The size of the chunks required as an
int
- A
boolean
to instruct the functions to preserve the keys of the original array or to not. Note: the default isfalse
.
# A real-life problem
Walk with me now through this scenario: my boss wants to know for each working week, the average profits from his shop. Each working week has 5 days.
So, let's say for argument's sake say we have just queried the last 20 days of shop sales from the shop's database. The data returned populate our array with 20 entries and therefore has 4 working weeks.
Now this leaves us with the problem at hand, we need to calculate the average sales across every 5 days for 4 weeks. Follow me through this next section to achieve the result.
# Average segments with array_chunk
We know our data array has 20 entries, and we know that we need an average of each week of sales (5 entries). Let's utilize array_chunk
with a little bit of extra native PHP to do the calculations.
$sales = [
250.70, 220.10, 233, 243.50, 255,
200, 300, 234, 350, 222,
237.99, 200.30, 150.98, 201, 209,
200, 300, 240, 203, 280,
];
// Split the array into groups of five,
// representing a 5 days working week.
$salesPerWeek = array_chunk($sales, 5);
// Map all items to their averages, week by week.
$averageSales = array_map(
fn(array $items) => array_sum($items) / count($items),
$salesPerWeek
);
Now, if we print the contents of $averageSales
we will get something like the following:
[
240.46,
261.2,
199.854,
244.6,
]
Let's break down the code for complete transparency:
- First, we have our array with 20 entries of sales data for each day.
- Next, we use
array_chunk
to split it into groups of five. - Then we use
array_map
on each chunk, and usearray_sum
to divide by the count of the chunk to give us the average.
And that is it!
This type of functionality could be used in many statistical applications that require segmentation. The example in this article tries to show you how array_chunk
works in layman terms with a bit of a pretend use-case behind it. I hope this was interesting, if you would like to see any more of my content, please check out my blog, https://www.codewall.co.uk/
Footnotes
Recommend
-
18
Earth ScienceA Large Chunk of Ice Has Torn Away From Menacing Iceberg A68aMODIS satellite image showing the new iceberg fragment. Image: Ste...
-
10
array_chunk in PHP By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps PHP developers pin...
-
13
Laravel Eloquent chunk () 与 chunkyById () 的区别Summer摈弃世俗浮躁,追求技术精湛
-
7
一、chunk简介 1.1 chunk是什么 MongoDB在Sharding模式下(对于Sharding不了解的可以参考shard介绍),通过Mongos向开启了shard分片的集合写入文档,这些文档会根据其shardKey分散...
-
5
时序数据库Influx-IOx源码学习八(Chunk持久化) - 刘涛华的个人空间 - OSCHINA - 中文开源技术交流社区 仅限深圳|现场揭秘:腾讯云原生数据...
-
6
chunk extend overlapping在堆中是一种比较常见的利用手段,其主要原理就是因为某些意外情况我们可以去修改一些已经申请或在空闲状态的堆块的大小,从而造成堆块重叠的情况,而这也就引发了一系列安全隐患。 本文涉及相关实验:
-
4
Ruth Hamilton says space rock came through the ceiling, landing on her pillow at her Golden, B.C., homeCBC News · Posted: Oct 12, 2021 11:30 AM PT | Last Updated: October 12
-
10
-
4
Files Permalink
-
7
webpack的chunk生成逻辑发布于 今天 15:34 刚接触webpack时,使用webpack打包后只会生成一个被称为bundle的文件,在慢慢熟悉webpack后,如果同时对于前端优化有一定...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK