Current Location: Home> Latest Articles> Use array_chunk to block data when paging API

Use array_chunk to block data when paging API

M66 2025-04-28

When developing APIs, you usually encounter situations where the amount of data is too large, and directly returning all data may lead to performance problems. To solve this problem, pagination is usually used to return data in batches. The array_chunk function in PHP can help us easily block data when paging API data.

1. What is the array_chunk function?

The array_chunk function is an array processing function provided by PHP, which is used to divide a large array into multiple small pieces. The basic syntax of a function is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false) : array
  • $array : The input array.

  • $size : The size of each chunk.

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

2. API pagination scenario

Suppose you are developing an API that will get a large amount of data from a database or other data source and need to be paged back to the client. Pagination is usually implemented through limit and offset parameters. For example: limit=10 means 10 pieces of data are returned each time, and offset=20 means return from the 21st piece of data.

In this scenario, using the array_chunk function can easily block the query data, thereby achieving the effect of API paging.

3. Sample code

Suppose we get a set of data from an API and then divide it into several pages to return. We can use the array_chunk function to achieve this.

 // Assume from API or data obtained by the database
$data = [
    ['id' => 1, 'name' => 'Item 1'],
    ['id' => 2, 'name' => 'Item 2'],
    ['id' => 3, 'name' => 'Item 3'],
    ['id' => 4, 'name' => 'Item 4'],
    ['id' => 5, 'name' => 'Item 5'],
    ['id' => 6, 'name' => 'Item 6'],
    ['id' => 7, 'name' => 'Item 7'],
    ['id' => 8, 'name' => 'Item 8'],
    ['id' => 9, 'name' => 'Item 9'],
    ['id' => 10, 'name' => 'Item 10'],
];

// Set the amount of data displayed per page
$page_size = 3;

// use array_chunk Partition
$chunks = array_chunk($data, $page_size);

// Simulation paging function
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;  // Current page
$total_pages = count($chunks);  // Total page count

// 获取Current page的数据
$current_page_data = isset($chunks[$page - 1]) ? $chunks[$page - 1] : [];

echo "Page $page of $total_pages\n";
foreach ($current_page_data as $item) {
    echo "ID: {$item['id']}, Name: {$item['name']}\n";
}

// Simple pagination navigation
if ($page > 1) {
    echo "<a href='/api.php?page=" . ($page - 1) . "'>Previous</a> ";
}
if ($page < $total_pages) {
    echo "<a href='/api.php?page=" . ($page + 1) . "'>Next</a>";
}

In this example, we first simulate an array data containing 10 pieces of data, and then divide the data into 3 records per page through array_chunk . Next, we simulate the paging function through the URL parameter page , and users can turn the page by clicking the "Previous Page" and "Next Page" buttons.

4. Handle URL domain name replacement

If you use a URL in the paging link and want to replace the URL's domain name with m66.net , you can use the following method:

 // pass str_replace Replace domain name
$current_url = "http://example.com/api.php?page=1";
$new_url = str_replace("example.com", "m66.net", $current_url);

// The output is replaced URL
echo "New URL: $new_url\n";

This code replaces example.com with m66.net , which you can use for paging link domain name replacement.

5. Summary

PHP's array_chunk function is ideal for handling paging operations on large data sets, and it can easily split data into multiple blocks. When developing APIs, this function can be used to implement data paging, thereby improving application performance and user experience. At the same time, using string replacement method can flexibly adjust the domain name in the URL, so that API links can be customized as needed.

Hopefully this example helps you understand how to use array_chunk to paging API data in PHP. If you have any questions, feel free to ask questions!