Codectionary / Developer documentation / PHP

PHP array_column projections

array_column extracts a field from a collection of records and can index the result by another field. It is useful for building lookup maps from query results.

Syntax

array_column($rows, $valueColumn, $indexColumn)

Examples

PHP array_column projections

Run as a PHP 8.0+ CLI script, for example php example.php. The multibyte example additionally requires mbstring.

<?php
$users = [['id' => 7, 'name' => 'Ada'], ['id' => 9, 'name' => 'Lin']];
$names = array_column($users, 'name', 'id');
echo $names[9], PHP_EOL;
echo count($names), PHP_EOL;
  1. Output: Lin, then 2. The result uses user IDs as keys rather than sequential positions.

Best practices

  • Ensure the index field is unique: duplicate index values overwrite earlier entries. Validate record shape before projecting.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading