Current Location: Home> Latest Articles> Functions of simulating the SELECT col1, col2 FROM tables in SQL

Functions of simulating the SELECT col1, col2 FROM tables in SQL

M66 2025-05-11

In PHP, array_column is a very convenient function that allows you to extract a column of data from a multidimensional array. This feature can sometimes simulate the SELECT col1, col2 FROM table operations in SQL queries, especially when processing database query results or similar data.

So, how to use the array_column function to achieve SQL query-like effects? Let's illustrate it with a specific example.

Example: Simulate SQL query function

Suppose we have the following array containing multiple records, which is similar to the result querying from the database:

 $data = [
    ['id' => 1, 'name' => 'Alice', 'age' => 30],
    ['id' => 2, 'name' => 'Bob', 'age' => 25],
    ['id' => 3, 'name' => 'Charlie', 'age' => 35],
    ['id' => 4, 'name' => 'David', 'age' => 40],
];

We want to extract the data of the two fields name and age from this array, similar to the SELECT name and age FROM table in SQL query.

Use array_column to extract data

The array_column function accepts three parameters:

  1. $array : The input multi-dimensional array.

  2. $column_key : The key of the column to be extracted.

  3. $index_key (optional): If specified, the result will be indexed by pressing this key.

We only need to use array_column to extract the name and age field data, the code is as follows:

 // extract name and age Two columns of data
$names = array_column($data, 'name');
$ages = array_column($data, 'age');

// Output result
print_r($names);
print_r($ages);

Output result:

 Array
(
    [0] => Alice
    [1] => Bob
    [2] => Charlie
    [3] => David
)

Array
(
    [0] => 30
    [1] => 25
    [2] => 35
    [3] => 40
)

This code extracts the values ​​of the two fields name and age in the array. If you need to extract these two columns of data at the same time, you can combine them into a new multi-dimensional array:

 // extract name and age Two columns of data,and combine it into a new array
$result = array_map(function($item) {
    return ['name' => $item['name'], 'age' => $item['age']];
}, $data);

// Output combination results
print_r($result);

Output result:

 Array
(
    [0] => Array
        (
            [name] => Alice
            [age] => 30
        )

    [1] => Array
        (
            [name] => Bob
            [age] => 25
        )

    [2] => Array
        (
            [name] => Charlie
            [age] => 35
        )

    [3] => Array
        (
            [name] => David
            [age] => 40
        )
)

In this way, we successfully simulated the effect of the SELECT name, age FROM table in SQL, extracted two columns of data and returned in the new format.

Summarize

The array_column function is a very powerful tool for extracting a column of data from a multidimensional array. In some cases, it can replace SELECT operations in SQL queries, especially when the data you are processing is already present in an array. By combining array_map , you can flexibly simulate the ability to extract multiple columns of data from SQL queries.

Hope this article can help you better understand how to use array_column in PHP to simulate SQL queries. If you have more questions or need further examples, feel free to ask questions!