PHP array_column Function
last modified March 13, 2025
The PHP array_column
function extracts a single column from a
multi-dimensional array or an array of objects. It's useful for data extraction.
Basic Definition
The array_column
function returns values from a single column in
the input array. It works with both arrays and objects.
Syntax: array_column(array $array, mixed $column_key, mixed $index_key = null): array
.
The column_key
specifies which column to extract.
Basic array_column Example
This shows how to extract a column of values from an array of associative arrays.
<?php $users = [ ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'], ['id' => 2, 'name' => 'Mary', 'email' => 'mary@example.com'], ['id' => 3, 'name' => 'Peter', 'email' => 'peter@example.com'] ]; $names = array_column($users, 'name'); print_r($names);
This extracts all names from the users array. The output will be an array containing ['John', 'Mary', 'Peter'].
Extracting with Index Key
You can specify an index key to use as keys in the resulting array.
<?php $users = [ ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'], ['id' => 2, 'name' => 'Mary', 'email' => 'mary@example.com'], ['id' => 3, 'name' => 'Peter', 'email' => 'peter@example.com'] ]; $emails = array_column($users, 'email', 'id'); print_r($emails);
This creates an array with IDs as keys and emails as values. The output will be [1 => 'john@example.com', 2 => 'mary@example.com', 3 => 'peter@example.com'].
Working with Objects
array_column can also extract public properties from objects.
<?php class User { public function __construct( public int $id, public string $name, public string $email ) {} } $users = [ new User(1, 'John', 'john@example.com'), new User(2, 'Mary', 'mary@example.com'), new User(3, 'Peter', 'peter@example.com') ]; $names = array_column($users, 'name'); print_r($names);
This extracts the name property from each User object. The output will be the same as the first example: ['John', 'Mary', 'Peter'].
Extracting Nested Data
array_column can extract values from nested arrays using dot notation.
<?php $products = [ ['id' => 1, 'details' => ['name' => 'Laptop', 'price' => 999]], ['id' => 2, 'details' => ['name' => 'Phone', 'price' => 699]], ['id' => 3, 'details' => ['name' => 'Tablet', 'price' => 399]] ]; $prices = array_column($products, 'details.price'); print_r($prices);
This extracts all prices from the nested details array. The output will be [999, 699, 399]. The dot notation accesses nested array elements.
Using array_column with array_map
Combine array_column with array_map for more complex data transformations.
<?php $users = [ ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'], ['id' => 2, 'name' => 'Mary', 'email' => 'mary@example.com'], ['id' => 3, 'name' => 'Peter', 'email' => 'peter@example.com'] ]; $formattedEmails = array_map( fn($email) => "Email: $email", array_column($users, 'email') ); print_r($formattedEmails);
This extracts emails and formats them with a prefix. The output will be ['Email: john@example.com', 'Email: mary@example.com', 'Email: peter@example.com'].
Best Practices
- Column Existence: Verify columns exist before extraction.
- Performance: Use for large datasets efficiently.
- Readability: Combine with other array functions carefully.
- Type Safety: Consider data types when extracting values.
Source
PHP array_column Documentation
This tutorial covered the PHP array_column
function with practical
examples showing its usage for data extraction scenarios.
Author
List all PHP Array Functions.