PHP 8 reference · Array Functions

PHP array_column() Function

Return the values from one column of a list of records, optionally keyed by another column.

PHP 8 Since 5.5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array

Parameters

ParameterDescription
array A list of arrays or objects (rows).
column_key The column to return; null returns whole rows.
index_key optional A column to use as the keys of the result.

Return value

An array of the column's values.

Examples

Pluck a columnTry it yourself
<?php
$users = [
    ["id" => 3, "name" => "Ada", "role" => "admin"],
    ["id" => 7, "name" => "Alan", "role" => "editor"],
];
print_r(array_column($users, "name"));
print_r(array_column($users, "role", "name"));

Output

Array
(
    [0] => Ada
    [1] => Alan
)
Array
(
    [Ada] => admin
    [Alan] => editor
)
Index rows by IDTry it yourself
<?php
$rows = [["id" => 3, "name" => "Ada"], ["id" => 7, "name" => "Alan"]];
$byId = array_column($rows, null, "id");
echo $byId[7]["name"], "\n";

Output

Alan

Version history

VersionChange
PHP 5.5Added
PHP 7.0Works with arrays of objects (public properties).

Was this page helpful?