Current Location: Home> Latest Articles> How to use PHP's array_chunk function in a pagination system to limit the number of displays per page and effectively control data display

How to use PHP's array_chunk function in a pagination system to limit the number of displays per page and effectively control data display

M66 2025-04-28

Paging is a common feature in many websites and applications, especially when the data volume is large, paging can not only improve loading speed, but also enhance the user experience. In PHP, we can use the array_chunk function to easily achieve pagination effect. This article will introduce how to use array_chunk to control the amount of data displayed per page and effectively control the data display.

1. What is the array_chunk function?

array_chunk is a function in PHP used to split an array into multiple small arrays. This function is ideal for use in paging systems, and can divide a large dataset into small subsets based on the specified number of pages.

array_chunk function syntax:

 array_chunk(array $array, int $length, bool $preserve_keys = false): array
  • $array : The original array to be split.

  • $length : The size of each subarray (i.e. the number displayed per page).

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

2. How to use the array_chunk function in a pagination system?

Example: Suppose we have an array of multiple web links that we want to display paging, with 5 links per page.

 <?php
// Sample data:Web link array
$links = [
    "https://m66.net/page1",
    "https://m66.net/page2",
    "https://m66.net/page3",
    "https://m66.net/page4",
    "https://m66.net/page5",
    "https://m66.net/page6",
    "https://m66.net/page7",
    "https://m66.net/page8",
    "https://m66.net/page9",
    "https://m66.net/page10",
];

// Number of displayed per page
$linksPerPage = 5;

// use array_chunk Functions split arrays into pages $linksPerPage Elements
$paginatedLinks = array_chunk($links, $linksPerPage);

// Assume that the current page is 2 Page
$currentPage = 2;

// 获取当前Page的链接数据
$currentLinks = $paginatedLinks[$currentPage - 1];

// 显示当前Page的数据
echo "1. {$currentPage} Page的链接:<br>";
foreach ($currentLinks as $link) {
    echo $link . "<br>";
}
?>

Code parsing:

  1. We first define an array $links containing multiple web links.

  2. Then set the number of links displayed per page $linksPerPage to 5.

  3. Use the array_chunk function to split the original array $links into multiple small arrays that display up to 5 links per page, and save them in the $paginatedLinks variable.

  4. Assuming the current page is page 2, the data of the current page is controlled through the $currentPage variable.

  5. Get the data of the current page by accessing $paginatedLinks[$currentPage - 1] and display it.

Output:

 1. 2 Page的链接:
https://m66.net/page6
https://m66.net/page7
https://m66.net/page8
https://m66.net/page9
https://m66.net/page10

3. How to implement pagination navigation?

Pagination is not just about displaying data, it also requires a pagination navigation to allow users to jump between different pages. Here is a simple example of how to implement paging navigation.

 <?php
// Total data volume
$totalLinks = count($links);

// 计算总Page数
$totalPages = ceil($totalLinks / $linksPerPage);

// 显示分Page导航
echo "分Page导航:<br>";

for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='?page=$i'>1. $i Page</a> ";
}
?>

Code parsing:

  1. Calculate the total amount of data count($links) .

  2. Use the ceil() function to calculate the total number of pages. If the total amount of data cannot be divisible by the number of each page, ceil() will round the result upwards.

  3. Use the for loop to generate paging links, allowing users to jump to different pages.

Output:

 分Page导航:
1. 1 Page 1. 2 Page 1. 3 Page 1. 4 Page

4. Process the display of the current page

To let users know about the page they are currently on, the current page can be highlighted in the paging navigation. For example, the current page can be bolded or added with a different style.

 <?php
echo "分Page导航:<br>";

for ($i = 1; $i <= $totalPages; $i++) {
    if ($i == $currentPage) {
        // 当前Page加粗显示
        echo "<strong>1. $i Page</strong> ";
    } else {
        echo "<a href='?page=$i'>1. $i Page</a> ";
    }
}
?>

Output:

 分Page导航:
1. 1 Page 1. 2 Page 1. 3 Page 1. 4 Page

5. Summary

Through the array_chunk function, we can easily divide a large data set into multiple small arrays to achieve pagination display. This not only reduces the pressure on page loading, but also improves the user's browsing experience. Combined with paging navigation, users can easily jump to different pages and view required data. I hope this article can help you understand how to use the array_chunk function to implement the paging function and further improve your website performance.