Current Location: Home> Latest Articles> How to Quickly Extract the ID Column Using PHP's array_column Function When Handling Paginated Data

How to Quickly Extract the ID Column Using PHP's array_column Function When Handling Paginated Data

M66 2025-06-15

In daily development, especially in backend management systems or data analysis platforms, we often need to process paginated data, such as extracting a specific column of data for further operations. In PHP, array_column is a very useful function that allows us to quickly extract values of a specified key from a two-dimensional array to form a new indexed array. This article will explain how to quickly extract the ID column when handling paginated data using array_column, with practical examples.

Scenario Description

Suppose we are developing an API and need to return paginated list data to the frontend. The data comes from a database query, and the returned data format looks like the following:

$users = [
    ['id' => 101, 'name' => 'Zhang San', 'email' => 'zhangsan@m66.net'],
    ['id' => 102, 'name' => 'Li Si', 'email' => 'lisi@m66.net'],
    ['id' => 103, 'name' => 'Wang Wu', 'email' => 'wangwu@m66.net'],
    // ...more data
];

Now, we need to quickly extract all the user id values from this dataset for subsequent operations like API aggregation, bulk queries, etc.

Using array_column to Extract the ID Column

array_column is a native PHP function. Its basic syntax is as follows:

array_column(array $input, mixed $column_key, mixed $index_key = null): array  

Where:

  • $input: The input two-dimensional array;

  • $column_key: The column name to be extracted;

  • $index_key (optional): The column to be used as the index for the returned array.

Since we only need to extract the id column, we can simply use it like this:

$userIds = array_column($users, 'id');  
print_r($userIds);  

The output will be:

Array  
(  
    [0] => 101  
    [1] => 102  
    [2] => 103  
)  

This way, we quickly get an array of IDs: [101, 102, 103].

Complete Example Applied to a Paginated API

Let’s apply array_column in an actual code scenario of a paginated API, simulating an API that returns user paginated data.

function getUserList($page = 1, $limit = 10) {  
    // Simulating database paginated query results  
    $allUsers = [  
        ['id' => 101, 'name' => 'Zhang San', 'email' => 'zhangsan@m66.net'],  
        ['id' => 102, 'name' => 'Li Si', 'email' => 'lisi@m66.net'],  
        ['id' => 103, 'name' => 'Wang Wu', 'email' => 'wangwu@m66.net'],  
        ['id' => 104, 'name' => 'Zhao Liu', 'email' => 'zhaoliu@m66.net'],  
        ['id' => 105, 'name' => 'Sun Qi', 'email' => 'sunqi@m66.net'],  
        // Assume more data  
    ];  
$pagedUsers = array_slice($allUsers, $offset, $limit);  

// Extract ID column  
$ids = array_column($pagedUsers, 'id');  

return [  
    'data' => $pagedUsers,  
    'ids' => $ids,  
    'pagination' => [  
        'page' => $page,  
        'limit' => $limit,  
        'total' => count($allUsers)  
    ]  
];  

}

// Test call
$result = getUserList(1, 3);
print_r($result);

The output will look like this:

Array  
(  
    [data] => Array  
        (  
            [0] => Array  
                (  
                    [id] => 101  
                    [name] => Zhang San  
                    [email] => zhangsan@m66.net  
                )  
            ...  
        )  
    (  
        [0] => 101  
        [1] => 102  
        [2] => 103  
    )  

[pagination] => Array  
    (  
        [page] => 1  
        [limit] => 3  
        [total] => 5  
    )  

)

Conclusion

Using array_column greatly simplifies the logic for extracting fields from multidimensional arrays, especially for structured arrays such as paginated data and table data. Compared to traditional looping methods, array_column results in cleaner, more efficient code, making it a valuable tool for PHP developers when handling data. The next time you work with paginated data, consider using it to improve your development efficiency.