Current Location: Home> Function Categories> array_column

array_column

Returns the value of a single column in the input array
Name:array_column
Category:Array
Programming Language:php
One-line Description:Returns the value of a single column in the input array.

PHP function: array_column

Function name

array_column

Applicable to PHP version

PHP 5.5.0 and above

Function description

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.

Function Syntax

 <span class="fun">array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array</span>

parameter

  • $array : Required. The multi-dimensional array of inputs (usually a record array).
  • $column_key : Required. You want to return the column name or index of the value. If set to null , the entire subarray is returned.
  • $index_key : optional. Used as a column name or index for the key to return the array.

Return value

Returns an array containing the specified column values. If $index_key is provided, its value is used as the key to return the array.

Example

 $records = [ [ &#39;id&#39; => 1, &#39;name&#39; => &#39;Alice&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], [ &#39;id&#39; => 2, &#39;name&#39; => &#39;Bob&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], [ &#39;id&#39; => 3, &#39;name&#39; => &#39;Charlie&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], ]; $result = array_column($records, &#39;email&#39;);<br>
print_r($result);<br>

Description of sample code

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] )
Similar Functions
Popular Articles