Current Location: Home> Latest Articles> How to paginate a 2D array through array_chunk

How to paginate a 2D array through array_chunk

M66 2025-04-25

In actual development, we often need to display a large two-dimensional array paging, such as on a list display page. PHP provides a very practical function array_chunk() , which can split a large array into multiple small arrays. In this tutorial, we will learn how to use the array_chunk() function to paginate a large 2D array.

Sample Scenario

Suppose we have a large two-dimensional array that contains a lot of user profile information. We want to display this information page, with 5 records displayed on each page. We will use array_chunk() to implement this function.

step:

  1. Prepare a large 2D array

    Suppose we have a two-dimensional array of 20 records, each record being a user information:

     <?php
    $users = [
        ['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
        ['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
        ['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
        ['id' => 4, 'name' => 'David', 'email' => 'david@m66.net'],
        ['id' => 5, 'name' => 'Eve', 'email' => 'eve@m66.net'],
        ['id' => 6, 'name' => 'Frank', 'email' => 'frank@m66.net'],
        ['id' => 7, 'name' => 'Grace', 'email' => 'grace@m66.net'],
        ['id' => 8, 'name' => 'Hannah', 'email' => 'hannah@m66.net'],
        ['id' => 9, 'name' => 'Ivy', 'email' => 'ivy@m66.net'],
        ['id' => 10, 'name' => 'Jack', 'email' => 'jack@m66.net'],
        ['id' => 11, 'name' => 'Kevin', 'email' => 'kevin@m66.net'],
        ['id' => 12, 'name' => 'Lily', 'email' => 'lily@m66.net'],
        ['id' => 13, 'name' => 'Mason', 'email' => 'mason@m66.net'],
        ['id' => 14, 'name' => 'Nina', 'email' => 'nina@m66.net'],
        ['id' => 15, 'name' => 'Oscar', 'email' => 'oscar@m66.net'],
        ['id' => 16, 'name' => 'Paul', 'email' => 'paul@m66.net'],
        ['id' => 17, 'name' => 'Quincy', 'email' => 'quincy@m66.net'],
        ['id' => 18, 'name' => 'Rachel', 'email' => 'rachel@m66.net'],
        ['id' => 19, 'name' => 'Sam', 'email' => 'sam@m66.net'],
        ['id' => 20, 'name' => 'Tina', 'email' => 'tina@m66.net']
    ];
    ?>
    
  2. Pagination using array_chunk()

    The array_chunk() function divides an array into multiple subarrays of a specified size. We can limit the records on each page to 5, and display them on pages:

     <?php
    // Displayed per page 5 Record
    $page_size = 5;
    
    // use array_chunk() Split large arrays into multiple small arrays
    $paged_users = array_chunk($users, $page_size);
    
    // Output paged user information
    echo '<pre>';
    print_r($paged_users);
    echo '</pre>';
    ?>
    

    In this way, $paged_users is a two-dimensional array, each subarray contains 5 user records. For example, the first page shows 5 records, the second page shows the next 5 records, and so on.

  3. Pagination display

    Suppose we need to display user information for each page on the web page. We can generate paging links dynamically by calculating the current number of pages and the total number of pages.

     <?php
    $total_pages = count($paged_users);
    $current_page = 1;  // Assume that it is the first page
    
    // Display user information on the current page
    echo 'Current page:' . $current_page . '<br>';
    foreach ($paged_users[$current_page - 1] as $user) {
        echo 'ID: ' . $user['id'] . ',Name: ' . $user['name'] . ',Mail: ' . $user['email'] . '<br>';
    }
    
    // Show paging links
    echo '<br>Pagination link:<br>';
    for ($i = 1; $i <= $total_pages; $i++) {
        echo '<a href="page.php?page=' . $i . '">1.' . $i . 'Page</a> ';
    }
    ?>
    

    Here we assume that the current number of pages is determined by querying the string (such as page.php?page=1 ). By dynamically generating links, users can click on different pages to view corresponding data.