array_column
Returns the value of a single column in the input array
array_column
PHP 5.5.0 and above
The array_column function returns the value of a specified column from a multidimensional array and is often used to extract specific fields from a record array.
<span class="fun">array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array</span>
Returns an array containing the specified column values. If $index_key is provided, its value is used as the key to return the array.
$records = [ [ 'id' => 1, 'name' => 'Alice', 'email' => '[email protected]' ], [ 'id' => 2, 'name' => 'Bob', 'email' => '[email protected]' ], [ 'id' => 3, 'name' => 'Charlie', 'email' => '[email protected]' ], ];
$result = array_column($records, 'email');<br>
print_r($result);<br>
In this example, array_column extracts the 'email' field of each subarray from $records , returning an indexed array composed of email addresses:
Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] )