You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现三个数组联合分页:每页展示5条对应数据

实现三个PHP数组的联合分页展示

一、合并数组为结构化数据

先把三个数组对应索引的元素组合成统一的二维数组,让数据结构更清晰,方便后续分页操作:

// 原始数组
$Array1 = [86456,86456,86456,86456,86456,86456,86456,86456,83981,83981];
$Array2 = [1,1,1,1,2,2,2,3,3,3];
$Array3 = [1,2,3,4,1,2,3,1,2,3];

// 合并为包含完整信息的数组
$combinedData = [];
foreach ($Array1 as $index => $tmdbId) {
    $combinedData[] = [
        'tmdb_id' => $tmdbId,
        'season' => $Array2[$index],
        'episode' => $Array3[$index]
    ];
}

二、编写分页逻辑

通过GET参数传递当前页码,计算每页数据的偏移量,确保分页的合法性:

// 分页配置
$perPage = 5; // 每页展示5条
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1; // 默认第一页
$totalItems = count($combinedData);
$totalPages = ceil($totalItems / $perPage); // 计算总页数

// 确保页码在合法范围内
$currentPage = max(1, min($currentPage, $totalPages));

// 截取当前页需要展示的数据
$offset = ($currentPage - 1) * $perPage;
$currentPageData = array_slice($combinedData, $offset, $perPage);

三、渲染表格与分页按钮

将当前页数据渲染为表格,同时生成分页切换按钮,点击时同步切换三组数据的对应片段:

<!DOCTYPE html>
<html>
<head>
    <title>剧集分页展示</title>
    <style>
        table { border-collapse: collapse; margin: 20px 0; }
        th, td { border: 1px solid #ddd; padding: 8px 15px; text-align: left; }
        .pagination { margin: 10px 0; }
        .pagination a { margin-right: 10px; padding: 6px 12px; border: 1px solid #ddd; text-decoration: none; color: #333; }
        .pagination a:hover { background-color: #f8f8f8; }
    </style>
</head>
<body>
    <?php
    // 渲染表格
    echo "<table>
            <tr>
                <th>TMDB ID</th>
                <th>Season Numbers</th>
                <th>Episodes Numbers</th>
            </tr>";
    foreach ($currentPageData as $item) {
        echo "<tr>
                <td>{$item['tmdb_id']}</td>
                <td>{$item['season']}</td>
                <td>{$item['episode']}</td>
              </tr>";
    }
    echo "</table>";

    // 渲染分页按钮
    echo "<div class='pagination'>";
    if ($currentPage > 1) {
        echo "<a href='?page=" . ($currentPage - 1) . "'>上一页</a> ";
    }
    if ($currentPage < $totalPages) {
        echo "<a href='?page=" . ($currentPage + 1) . "'>下一页</a>";
    }
    echo "</div>";
    ?>
</body>
</html>

四、完整整合代码

把上述部分整合起来就是可以直接运行的完整代码,放在PHP文件中即可实现需求:

<?php
// 原始数组
$Array1 = [86456,86456,86456,86456,86456,86456,86456,86456,83981,83981];
$Array2 = [1,1,1,1,2,2,2,3,3,3];
$Array3 = [1,2,3,4,1,2,3,1,2,3];

// 合并为结构化数组
$combinedData = [];
foreach ($Array1 as $index => $tmdbId) {
    $combinedData[] = [
        'tmdb_id' => $tmdbId,
        'season' => $Array2[$index],
        'episode' => $Array3[$index]
    ];
}

// 分页配置
$perPage = 5;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$totalItems = count($combinedData);
$totalPages = ceil($totalItems / $perPage);

// 页码合法性校验
$currentPage = max(1, min($currentPage, $totalPages));

// 截取当前页数据
$offset = ($currentPage - 1) * $perPage;
$currentPageData = array_slice($combinedData, $offset, $perPage);
?>

<!DOCTYPE html>
<html>
<head>
    <title>TMDB 剧集分页展示</title>
    <style>
        table { border-collapse: collapse; margin: 20px 0; }
        th, td { border: 1px solid #ddd; padding: 8px 15px; text-align: left; }
        .pagination { margin: 10px 0; }
        .pagination a { margin-right: 10px; padding: 6px 12px; border: 1px solid #ddd; text-decoration: none; color: #333; }
        .pagination a:hover { background-color: #f8f8f8; }
    </style>
</head>
<body>
    <?php
    // 渲染表格
    echo "<table>
            <tr>
                <th>TMDB ID</th>
                <th>Season Numbers</th>
                <th>Episodes Numbers</th>
            </tr>";
    foreach ($currentPageData as $item) {
        echo "<tr>
                <td>{$item['tmdb_id']}</td>
                <td>{$item['season']}</td>
                <td>{$item['episode']}</td>
              </tr>";
    }
    echo "</table>";

    // 渲染分页按钮
    echo "<div class='pagination'>";
    if ($currentPage > 1) {
        echo "<a href='?page=" . ($currentPage - 1) . "'>上一页</a> ";
    }
    if ($currentPage < $totalPages) {
        echo "<a href='?page=" . ($currentPage + 1) . "'>下一页</a>";
    }
    echo "</div>";
    ?>
</body>
</html>

内容的提问来源于stack exchange,提问作者David Chriss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:23:13