❮ PHP Array Reference
Example
Get column of last names from a recordset:
Output:
Array
[
[0] => Griffin
[1] => Smith
[2] => Doe
]
Definition and Usage
The array_column[] function returns the values from a single column in the input array.
Syntax
array_column[array, column_key, index_key]
Parameter Values
array | Required. Specifies the multi-dimensional array [record-set] to use. As of PHP 7.0, this can also be an array of objects. |
column_key | Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays [useful together with index_key to re-index the array] |
index_key | Optional. The column to use as the index/keys for the returned array |
Technical Details
Returns an array of values that represents a single column from the input array |
5.5+ |
More Examples
Example
Get column of last names from a recordset, indexed by the "id" column:
Output:
Array
[
[5698] => Griffin
[4767] => Smith
[3809] => Doe
]
❮ PHP Array Reference
Definition and Usage
The array_column[] function returns the values from a single column of the input array and identified by the column_key.
Optionally, you can pass index_key to index the values in the returned array by the values from the index_key column of the input array.
Syntax
array array_column[ array $input , mixed $column_key [, mixed $index_key = NULL ] ]
Parameters
1 | input [mandatory] A multi-dimensional array or an array of objects from which to pull a column of values from. |
2 | column_key [mandatory] The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. This value can be NULL to return complete arrays or objects |
3 | index_key [optional] The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. |
Return Values
The function array_column returns an array of values representing a single column from the input array.
PHP Version
This function was first introduced in PHP Version 5.5.0. The the ability for the input parameter to be an array of objects was introduced in 7.0.0
Example
Try out following example to get the column of first names from a recordset −
This will produce following result −
Array [ [0] => Zara [1] => Nuha [2] => Shifa [3] => Riya ]
Example
Now let's try one more example to get the column of first names from a recordset but this time we will index recordset using id −
This will produce following result −
Array [ [2135] => Zara [3245] => Nuha [5342] => Shifa [5623] => Riya ]
php_function_reference.htm