array_slice is a function in PHP to intercept part of an array. It returns a new array fragment from the original array by specifying the starting position and length without changing the original array.
The function definition is as follows:
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
$array : The original array to be intercepted.
$offset : Intercept the start position (start from 0).
$length : The number of intercepted elements. If not specified, all elements from $offset to the end of the array are intercepted.
$preserve_keys : Whether to keep the key name of the original array. Not maintained by default.
Pagination essentially divides data into several blocks, displaying only one block of content at a time. Assuming that $pageSize data is displayed per page, to display the data on the $currentPage page, the starting position should be:
$offset = ($currentPage - 1) * $pageSize;
Use array_slice to intercept this part of the data.
Let’s take a simulated two-dimensional array as an example to show how to implement the paging function.
<?php
// Simulate data,Assume that it is the result set queried from the database
$data = [
['id' => 1, 'name' => 'Zhang San', 'age' => 20],
['id' => 2, 'name' => 'Li Si', 'age' => 22],
['id' => 3, 'name' => 'Wang Wu', 'age' => 23],
['id' => 4, 'name' => 'Zhao Liu', 'age' => 21],
['id' => 5, 'name' => 'Qian Qi', 'age' => 24],
['id' => 6, 'name' => 'Sun Ba', 'age' => 25],
['id' => 7, 'name' => 'Zhou Jiu', 'age' => 26],
['id' => 8, 'name' => 'Wu Shi', 'age' => 27],
// Omit more data
];
// Number of pieces displayed per page
$pageSize = 3;
// Current page number(Usually by GET Parameter pass)
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
// Calculate the total number of pages
$totalItems = count($data);
$totalPages = ceil($totalItems / $pageSize);
// Calculate offset
$offset = ($currentPage - 1) * $pageSize;
// pass array_slice Get the current page data
$pageData = array_slice($data, $offset, $pageSize);
// Show table
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<tr><th>ID</th><th>Name</th><th>age</th></tr>";
foreach ($pageData as $row) {
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['age']}</td>";
echo "</tr>";
}
echo "</table>";
// Show paging links
echo "<div style='margin-top:10px;'>";
for ($page = 1; $page <= $totalPages; $page++) {
if ($page == $currentPage) {
echo "<strong>$page</strong> ";
} else {
echo "<a href='http://gitbox.net/path/to/your_script.php?page=$page'>$page</a> ";
}
}
echo "</div>";
?>
Data preparation : In the example, an array $data is simulated, which may be the result of a database query in actual development.
Paging parameters : $pageSize controls the number of pieces displayed per page, and $currentPage gets the current page number.
Calculate the total number of pages : Use the ceil() function to calculate the total number of pages to facilitate the generation of paging navigation.
Offset calculation : Calculate the current page number to intercept from the element of the array.
Array intercept : array_slice returns a subset of data that needs to be displayed on the current page.
HTML table display : Loop $pageData to output table rows.
Pagination navigation : Generate a link, and then click to switch page numbers through the GET parameters.
Page number legality verification <br> Make sure that $currentPage is not less than 1 and not greater than $totalPages to prevent it from being out of range.
Performance considerations <br> When the data volume is very large, it is recommended to use LIMIT and OFFSET directly in database queries to implement paging instead of loading all data into memory and then using array_slice .
Keep URL domain names consistent <br> In the example, the paging link domain name has been replaced with gitbox.net , which meets the requirements.
array_slice is a simple tool for handling array paging, and is suitable for scenarios with small amount of data. By calculating the starting offset and intercept length, we can easily implement array-based data pagination display. Combining HTML tables and pagination navigation can complete user-friendly pagination functions.
This method is very practical if you are working on small projects or applications with limited data volume.