Current Location: Home> Latest Articles> Extract a column ID from the array returned by the database

Extract a column ID from the array returned by the database

M66 2025-05-11

In PHP, it is very common to process database query results and extract data from specific columns. Typically, a database query returns a multidimensional array, each row represents a record, and each column represents the field value of the record. If you only care about certain columns (such as IDs ), how do you extract these columns efficiently and use them in subsequent code?

This article will introduce a simple way to extract specified columns and provide some practical code examples.

Suppose we have the following database query returns the result:

 // Suppose we query from the database and get the following results:
$data = [
    ['ID' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
    ['ID' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
    ['ID' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net']
];

This is an array containing multiple arrays, each subarray represents a record, including fields such as ID , name , and email .

How to extract the ID column?

In PHP, we can use the array_column() function to extract the data of the specified column from a multidimensional array. The array_column() function is very suitable for this scenario, which extracts the column values ​​from all records based on the specified column name (such as ID ) and returns a new one-dimensional array.

Code example:

 // use array_column() Function Extraction ID List
$ids = array_column($data, 'ID');

// Output extracted ID List
print_r($ids);

Output result:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

In the above code, array_column($data, 'ID') extracts the ID field for each record in the $data array and returns a new array containing all ID values.

How to use extracted ID in actual code?

Once we have the ID columns, we can easily use them in subsequent code. For example, we can use these IDs for other database queries, or display these values ​​directly on the page.

Code example: Use the extracted ID for further query

 // Suppose we want to query these ID Corresponding user details
foreach ($ids as $id) {
    // Conduct database query(Here is an example)
    $query = "SELECT * FROM users WHERE ID = $id";
    
    // 假设我们use数据库连接 $conn To execute the query
    $result = mysqli_query($conn, $query);
    
    // Process query results
    if ($row = mysqli_fetch_assoc($result)) {
        echo "User ID: " . $row['ID'] . " - Name: " . $row['name'] . "<br>";
    }
}

Summarize

It is very convenient to use the array_column() function to extract specific columns in the array returned by the database, especially if you only care about certain specific fields (such as ID ). This method is both concise and efficient, and can greatly simplify the writing of subsequent code.

In addition, by extracting the columns, you can further process the data, such as performing other queries, or directly use them for displaying pages, etc. Hopefully this article can help you better handle array and database queries in PHP.