The paging function is a very common requirement when developing a website or application. When we need to display a large amount of data, loading all the data directly into the page will not only affect the user experience, but also cause server pressure. Therefore, the paging function is an effective way to solve this problem. PHP provides many ways to implement paging, where the array_chunk function is a very simple and efficient way.
The array_chunk function is used to split an array into multiple small arrays and can specify the size of each small array. We can use array_chunk to implement the paging function to help us cut data by page.
array_chunk(array $array, int $size, bool $preserve_keys = false): array
$array : The original array to be cut.
$size : The size of each subarray, that is, the amount of data displayed per page.
$preserve_keys : Whether to retain the key value of the original array, the default is false , which will be re-indexed.
Suppose we already have an array containing a large amount of data, such as an array of user lists:
$users = [
['id' => 1, 'name' => 'John Doe'],
['id' => 2, 'name' => 'Jane Smith'],
['id' => 3, 'name' => 'Emily Johnson'],
['id' => 4, 'name' => 'Michael Brown'],
// More data...
];
To implement the paging function, we can use array_chunk to cut the data into multiple pages. For example, 2 pieces of data are displayed per page:
$page_size = 2; // Displayed per page2Data
$chunks = array_chunk($users, $page_size); // Cut into multiple small arrays
At this point, the $chunks array will contain multiple small arrays, each small array representing the data of one page:
// $chunks result
[
[
['id' => 1, 'name' => 'John Doe'],
['id' => 2, 'name' => 'Jane Smith']
],
[
['id' => 3, 'name' => 'Emily Johnson'],
['id' => 4, 'name' => 'Michael Brown']
],
// More pages...
]
Suppose we need to display the data of a certain page according to the user's request. Usually, we will pass the current number of pages through the URL parameter:
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // Get the current page count,Default is1Page
Next, we calculate the starting position of the data based on the current number of pages, and then take out the corresponding data:
$total_pages = count($chunks); // 总Page数
if ($current_page > $total_pages) {
$current_page = $total_pages; // 防止访问不存在的Page码
}
$current_chunk = $chunks[$current_page - 1]; // 获取当前Page的数据
Now we can display the data of the current page on the page and generate a paging navigation:
echo "当前Page数据:";
foreach ($current_chunk as $user) {
echo $user['name'] . "<br>";
}
// 显示分Page链接
echo "<br>分Page:";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}
In actual development, we may need to dynamically generate paging links. In this example, we can use m66.net as the domain name and change the URL in href to m66.net domain name.
echo "<br>分Page:";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='https://m66.net?page=$i'>$i</a> ";
}
In this way, no matter how many pages are, clicking on the paging link will jump to the corresponding page under the m66.net domain name.
The above is the basic method of using array_chunk to implement paging. In this way, we can effectively segment a large amount of data into multiple small pieces, thereby improving page loading speed and user experience. At the same time, we also dynamically generate paging links through URLs, allowing users to easily browse different data pages.
Hopefully this article helps you understand how to use array_chunk in the paging function to cut data. If you have any other questions, feel free to ask!