Current Location: Home> Latest Articles> Use array_chunk to split array data by hourly, daily and other time periods

Use array_chunk to split array data by hourly, daily and other time periods

M66 2025-04-26

In PHP, the array_chunk function can split a large array into multiple small arrays. Although array_chunk is usually grouped by a fixed length of an array, in some scenarios, we may need to group the array by time period (such as hourly, daily, etc.). This article will introduce how to implement arrays by time period through array_chunk .

1. What is the array_chunk function?

array_chunk is a function in PHP that divides a large array into multiple small arrays. Its basic syntax is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false): array
  • $array : an array that needs to be split.

  • $size : The maximum number of elements per small array.

  • $preserve_keys : Whether to retain the key name of the original array, the default is false .

2. Group arrays by time period

Suppose we have an array of timestamps that need to be grouped by hourly or daily time periods. We can use array_chunk to help us achieve this.

Grouped by hour

First, we can create an array of timestamps and group them by hour. What we need to do is convert the timestamp to an hour and group the timestamps with the same hour into a group.

 // Simulate timestamp array
$timestamps = [
    1617763200, // 2021-04-07 00:00:00
    1617766800, // 2021-04-07 01:00:00
    1617770400, // 2021-04-07 02:00:00
    1617802800, // 2021-04-07 06:00:00
    1617806400, // 2021-04-07 07:00:00
    1617810000, // 2021-04-07 08:00:00
    1617813600  // 2021-04-07 09:00:00
];

// Group timestamps by hour
$groupedByHour = [];
foreach ($timestamps as $timestamp) {
    $hour = date('Y-m-d H', $timestamp); // Format the timestamp by hour
    $groupedByHour[$hour][] = $timestamp;
}

// Output an array grouped by hours
print_r($groupedByHour);

The above code will group the timestamps according to the hour part of each timestamp, and the output arrays will be grouped by hours, for example:

 Array
(
    [2021-04-07 00] => Array
        (
            [0] => 1617763200
        )

    [2021-04-07 01] => Array
        (
            [0] => 1617766800
        )

    [2021-04-07 02] => Array
        (
            [0] => 1617770400
        )

    [2021-04-07 06] => Array
        (
            [0] => 1617802800
        )

    [2021-04-07 07] => Array
        (
            [0] => 1617806400
        )

    [2021-04-07 08] => Array
        (
            [0] => 1617810000
        )

    [2021-04-07 09] => Array
        (
            [0] => 1617813600
        )
)

Group by daily

We can also group timestamps by day, so that each group will contain all timestamps in one day.

 // Group timestamps by day
$groupedByDay = [];
foreach ($timestamps as $timestamp) {
    $day = date('Y-m-d', $timestamp); // Format the timestamp by day
    $groupedByDay[$day][] = $timestamp;
}

// Output array grouped by day
print_r($groupedByDay);

The output result will be grouped by days:

 Array
(
    [2021-04-07] => Array
        (
            [0] => 1617763200
            [1] => 1617766800
            [2] => 1617770400
            [3] => 1617802800
            [4] => 1617806400
            [5] => 1617810000
            [6] => 1617813600
        )
)

3. Summary

With array_chunk we can easily group an array of timestamps by time periods (such as hourly, daily, etc.). Although array_chunk is usually used to group by number of elements, here we use the date() function to format the timestamps into the time period we need, and then group them by these time periods.