Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_column()

Extract one field from each record and optionally index the resulting values by another field.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Extract one field from each record and optionally index the resulting values by another field. Rows lacking the requested value field are skipped. When two records provide the same index, the later value wins. A null value-column argument selects each entire row.

Signature

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

Example output

{"2":"B"}

Examples

<?php
$rows = [['id'=>2,'tag'=>'A'], ['id'=>3], ['id'=>2,'tag'=>'B']];
echo json_encode(array_column($rows, 'tag', 'id'));

Result

{"2":"B"}

Sources