Current Location: Home> Latest Articles> Use array_column to extract multiple columns of multi-dimensional arrays

Use array_column to extract multiple columns of multi-dimensional arrays

M66 2025-04-29

In PHP, the array_column function can easily extract specific column data from a multidimensional array. It is often used to extract a specified column from an associative array or multidimensional array for subsequent processing. Next, we will dive into how to use the array_column function to extract multi-column data from a multi-dimensional array.

1. Basic usage

The basic syntax of the array_column function is as follows:

 array_column(array $array, $column_key, $index_key = null): array
  • $array : The input multi-dimensional array.

  • $column_key : The key name of the column that needs to be extracted.

  • $index_key : (optional) is used as the key to return the array. If not provided, the returned array will be an indexed array.

Example 1: Extracting single column data

First, let's look at a simple example showing how to extract single column data from a multidimensional array:

 <?php
$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'],
];

$emails = array_column($data, 'email');

print_r($emails);
?>

Output result:

 Array
(
    [0] => alice@m66.net
    [1] => bob@m66.net
    [2] => charlie@m66.net
)

In this example, we use array_column to extract the email column.

2. Extract multiple columns of data

array_column can only extract one column of data, but if you want to extract multiple columns of data, you can use the combination of array_column and array_map to achieve it. Here is an example showing how to extract multiple columns:

Example 2: Extracting multiple columns of data

 <?php
$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'],
];

$namesAndEmails = array_map(function($item) {
    return [
        'name'  => $item['name'],
        'email' => $item['email'],
    ];
}, $data);

print_r($namesAndEmails);
?>

Output result:

 Array
(
    [0] => Array
        (
            [name] => Alice
            [email] => alice@m66.net
        )
    [1] => Array
        (
            [name] => Bob
            [email] => bob@m66.net
        )
    [2] => Array
        (
            [name] => Charlie
            [email] => charlie@m66.net
        )
)

In this example, we use the array_map function to extract the name and email columns into a new array.

3. Use array_column to extract multiple columns of data

If you want to extract multiple column data through array_column , you can usually use it in conjunction with array_column with some other functions. For example, if you only care about one column and need other data for correlation operations, here is a more complex example.

Example 3: Extract and reorganize data from a multidimensional array

 <?php
$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'],
];

$names = array_column($data, 'name');
$emails = array_column($data, 'email');

// Merge two arrays
$merged = array_map(null, $names, $emails);

print_r($merged);
?>

Output result:

 Array
(
    [0] => Array
        (
            [0] => Alice
            [1] => alice@m66.net
        )
    [1] => Array
        (
            [0] => Bob
            [1] => bob@m66.net
        )
    [2] => Array
        (
            [0] => Charlie
            [1] => charlie@m66.net
        )
)

In this example, we use array_column to extract the name and email columns, and then merge them into a two-dimensional array through the array_map function.

4. Summary

  • array_column is a very powerful tool that can help you easily extract a column of data in an array.

  • For multiple column extraction, although array_column can only handle single columns, you can realize the requirement of extracting multiple columns by combining other functions such as array_map .

  • array_column is more efficient, especially when dealing with large-scale data, which is more efficient than manual loops.

By using array_column and other array functions reasonably, you can process multidimensional array data in PHP more efficiently.