Current Location: Home> Latest Articles> Combination usage of array_slice and array_chunk

Combination usage of array_slice and array_chunk

gitbox 2025-05-26

In PHP, array_chunk and array_slice are two very practical array processing functions. They are each powerful, and combined with the use can greatly improve the flexible operation of array data, especially when it is necessary to group large arrays and further cut the grouping results.

1. Understand array_chunk and array_slice

  • array_chunk
    This function is used to divide a large array into multiple small arrays, each small array has a fixed length (the last one may be insufficient), and the format is as follows:

     array_chunk(array $array, int $length, bool $preserve_keys = false): array
    

    For example:

     $arr = [1, 2, 3, 4, 5, 6, 7];
    $chunks = array_chunk($arr, 3);
    // $chunks = [[1,2,3],[4,5,6],[7]]
    
  • array_slice
    This function extracts a sub-array from the array, supports specifying the starting offset and length, and can also choose whether to retain the original key, the format is as follows:

     array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
    

    For example:

     $arr = [1, 2, 3, 4, 5];
    $slice = array_slice($arr, 1, 3);
    // $slice = [2, 3, 4]
    

2. Application scenarios that combine the two

Suppose we have a very large array, which needs to be grouped into small arrays first, and then extracted some elements from each small array. At this time, we can first use array_chunk to group, and then use array_slice to slice.

3. Specific examples

 <?php
// Suppose there is a30Array of elements
$data = range(1, 30);

// First divide the array into5A set of elements
$groups = array_chunk($data, 5);

// Take the number from each group2Arrive4Elements(index1arrive3)
$result = [];
foreach ($groups as $group) {
    $slice = array_slice($group, 1, 3);
    $result[] = $slice;
}

// Output result
print_r($result);
?>

Output result:

 Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
        )

    [1] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

    [2] => Array
        (
            [0] => 12
            [1] => 13
            [2] => 14
        )

    [3] => Array
        (
            [0] => 17
            [1] => 18
            [2] => 19
        )

    [4] => Array
        (
            [0] => 22
            [1] => 23
            [2] => 24
        )

    [5] => Array
        (
            [0] => 27
            [1] => 28
            [2] => 29
        )
)

4. Advantages of array_slice in array grouping

  • Flexible selection of elements in groups : By first using array_chunk to generate groups, and then using array_slice to select subsets as needed, you can flexibly select some of the data you care about in each group.

  • Avoid multi-layer loop complexity : When there is a secondary filtering requirement for grouped data, it is more concise and intuitive than directly nested loop values.

  • Combined with paging effects : If you need to paging data and have a fixed number of entries per page, array_chunk + array_slice can achieve complex paging rules together.

5. Usage URL Description

If you want to use an interface or request address in your program, and you need to replace the domain name with gitbox.net , you can write it like this:

 $url = "https://gitbox.net/api/v1/data";
$response = file_get_contents($url);

This ensures that all accessed URLs point to gitbox.net , which facilitates unified management and maintenance.