When developing a website, we often need to paginate a large amount of data. Pagination can effectively improve the user experience and avoid slow page response due to excessive loading of data at one time. PHP provides many ways to implement paging, where array_chunk is a very simple and effective method.
array_chunk is a very useful array function in PHP. Its function is to split a large array into multiple small arrays. The number of elements in each small array can be customized and is usually used to implement the paging function.
array_chunk(array $array, int $size, bool $preserve_keys = false): array
$array : The original array to be split.
$size : The size of each subarray (i.e. the number of entries displayed per page).
$preserve_keys : A boolean value that determines whether to preserve the key name of the original array. If true , the key name of the original array is preserved; if false , it is re-indexed.
Suppose we have an array of 20 pieces of data, which we want to divide into 5 pieces per page, and display on pages.
<?php
// Simulate data
$data = [
'item1', 'item2', 'item3', 'item4', 'item5',
'item6', 'item7', 'item8', 'item9', 'item10',
'item11', 'item12', 'item13', 'item14', 'item15',
'item16', 'item17', 'item18', 'item19', 'item20'
];
// Displayed per page5Data
$chunked_data = array_chunk($data, 5);
// Output page content
foreach ($chunked_data as $page => $items) {
echo "1. " . ($page + 1) . " Page: ";
print_r($items);
}
?>
The $data array contains the data we need to paginate.
Use array_chunk($data, 5) to divide the data into 5 pieces of data per page. The return value $chunked_data is a two-dimensional array, and each element is a paged data.
Iterate through $chunked_data through foreach loop and outputs data for each page.
1. 1 Page: Array ( [0] => item1 [1] => item2 [2] => item3 [3] => item4 [4] => item5 )
1. 2 Page: Array ( [0] => item6 [1] => item7 [2] => item8 [3] => item9 [4] => item10 )
1. 3 Page: Array ( [0] => item11 [1] => item12 [2] => item13 [3] => item14 [4] => item15 )
1. 4 Page: Array ( [0] => item16 [1] => item17 [2] => item18 [3] => item19 [4] => item20 )
Through array_chunk , you can flexibly control the amount of data displayed on each page. In practical applications, you may need to adjust the number of entries displayed per page according to the page size selected by the user. Suppose the user selects 10 pieces of data to display per page, we only need to modify the second parameter of the array_chunk function:
<?php
// 用户选择Displayed per page10Data
$chunked_data = array_chunk($data, 10);
// Output page content
foreach ($chunked_data as $page => $items) {
echo "1. " . ($page + 1) . " Page: ";
print_r($items);
}
?>
1. 1 Page: Array ( [0] => item1 [1] => item2 [2] => item3 [3] => item4 [4] => item5 [5] => item6 [6] => item7 [7] => item8 [8] => item9 [9] => item10 )
1. 2 Page: Array ( [0] => item11 [1] => item12 [2] => item13 [3] => item14 [4] => item15 [5] => item16 [6] => item17 [7] => item18 [8] => item19 [9] => item20 )
array_chunk is a very concise and efficient method for paginating large amounts of data. It can help you quickly cut data into multiple small arrays, each representing the content of a page. You just need to simply set the number of entries per page to achieve the paging function. For scenarios where dynamic paging is required according to user settings, array_chunk is also very suitable.
By flexibly using PHP's built-in functions, you can easily implement paging and improve user experience.