PHP array_column() function


The array_column() function in PHP is used to return the values of a specified column from a multi-dimensional array, effectively extracting a single column from an array of arrays. This function is particularly useful when working with datasets, such as database query results, where you want to retrieve specific fields from each entry.

Syntax:

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

Parameters:

  • $input: The multi-dimensional array (array of arrays) from which you want to extract the column.
  • $column_key: The key of the column you want to retrieve. This can be a string (for associative arrays) or an integer (for indexed arrays).
  • $index_key: (Optional) If specified, this parameter allows you to set the keys of the returned array using the values from another column of the input array. This is also a string or an integer.

Return Value:

  • The function returns an array of values from the specified column. If the column does not exist, it returns an empty array. If the input array is empty, it also returns an empty array.

Example 1: Basic Usage

Here's a simple example demonstrating how to use array_column().

<?php $records = [ ["id" => 1, "name" => "Alice", "age" => 25], ["id" => 2, "name" => "Bob", "age" => 30], ["id" => 3, "name" => "Charlie", "age" => 35], ]; // Extract the 'name' column $names = array_column($records, 'name'); print_r($names); ?>

Output:

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

In this example, the name column is extracted from the $records array.

Example 2: Specifying an Index Key

You can use the third parameter to specify which column to use as the index for the returned array.

<?php $records = [ ["id" => 1, "name" => "Alice", "age" => 25], ["id" => 2, "name" => "Bob", "age" => 30], ["id" => 3, "name" => "Charlie", "age" => 35], ]; // Extract the 'name' column and use 'id' as the index $names_by_id = array_column($records, 'name', 'id'); print_r($names_by_id); ?>

Output:

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

In this example, the name column is extracted, and the id column is used as the index for the returned array, resulting in names being keyed by their respective IDs.

Example 3: Handling Missing Columns

If the specified column does not exist in any of the arrays, it simply returns an empty array.

<?php $records = [ ["id" => 1, "name" => "Alice", "age" => 25], ["id" => 2, "name" => "Bob", "age" => 30], ]; // Attempting to extract a non-existent 'salary' column $salary = array_column($records, 'salary'); print_r($salary); ?>

Output:

Array ( )

Example 4: Multi-dimensional Data

If the input array contains nested arrays, array_column() can be used to extract values from deeper levels.

<?php $users = [ ["id" => 1, "info" => ["name" => "Alice", "age" => 25]], ["id" => 2, "info" => ["name" => "Bob", "age" => 30]], ]; // Extract the 'name' column from the 'info' sub-array $names = array_column($users, 'info')['name']; print_r($names); ?>

Output:

Array ( [0] => Alice [1] => Bob )

Practical Usage:

  • The array_column() function is extremely useful in situations such as:
    • Extracting specific fields from database query results, making it easier to work with the data.
    • Creating lists or dropdowns from data where only certain attributes are needed.
    • Simplifying data manipulation tasks by allowing easy access to specific properties of objects or arrays.

Summary:

  • array_column($input, $column_key, $index_key) retrieves values from a specified column of a multi-dimensional array.
  • The function is efficient for extracting data and can also re-index the resulting array based on another column.
  • It's particularly helpful for simplifying data handling and processing tasks in PHP applications.