PHP array_column function tutorial shows how to extract columns from arrays in PHP. Learn array_column with practical examples.
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.
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.
This shows how to extract a column of values from an array of associative arrays.
basic_array_column.php
<?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’].
You can specify an index key to use as keys in the resulting array.
index_key_example.php
<?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’].
array_column can also extract public properties from objects.
object_properties.php
<?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’].
array_column can extract values from nested arrays using dot notation.
nested_data.php
<?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.
Combine array_column with array_map for more complex data transformations.
with_array_map.php
<?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’].
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.
PHP array_column Documentation
This tutorial covered the PHP array_column function with practical examples showing its usage for data extraction scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.