Home >>PHP Array Functions >PHP array_column() Function
PHP array_column() Function is used to get the values from a single column in the given input array. It accepts three parameters in which two are mandatory and one is optional. It returns an array as output containing the values from a single column of the input array.
Syntax:
array_column($array, $column_key, $index_key);
Parameter | Description |
---|---|
array | This is a required parameter. This parameter defines the multi-dimensional array to use. |
column_key | This is a required parameter. This parameter definesan integer key or a string key name of the column of values to return. |
index_key | This is an optional parameter. This parameter defines the column to use as the indexkeys for the returned array. |
Here is an example of array_column() function in PHP:
<html> <body> <pre> <?php $a = array( array( 'id' => 01, 'first_name' => 'Abhimanyu', 'last_name' => 'Sharma', ), array( 'id' => 02, 'first_name' => 'Rishabh', 'last_name' => 'Pant', ), array( 'id' => 03, 'first_name' => 'Jack', 'last_name' => 'Daniel', ) ); $last_names = array_column($a, 'last_name'); print_r($last_names); ?> </pre> </body> </html>
Array ( [0] => Sharma [1] => Pant [2] => Daniel )
Example 2:
<html> <body> <pre> <?php $a = array( array( 'id' => 01, 'first_name' => 'Abhimanyu', 'last_name' => 'Sharma', ), array( 'id' => 02, 'first_name' => 'Rishabh', 'last_name' => 'Pant', ), array( 'id' => 03, 'first_name' => 'Jack', 'last_name' => 'Daniel', ) ); $first_names = array_column($a, 'first_name'); print_r($first_names); $last_names = array_column($a, 'last_name'); print_r($last_names); ?> </pre> </body> </html>
Array ( [0] => Abhimanyu [1] => Rishabh [2] => Jack ) Array ( [0] => Sharma [1] => Pant [2] => Daniel )