PHP Array column() Function

It is an inbuilt function in PHP. The array_column() function is used to return the values from a single column in the input array. This function is introduced in PHP 5.5.

Syntax


array_column(array,column_key,index_key); 
        

Parameters

Parameter Description Is compulsory
Column_key This parameter may be an integer or a string key name of the column of values to return. It may also be NULL to return complete arrays (useful together with index_key to re-index the array) Compulsory
index_key This parameter may be an integer key or a string key name of the column. It is used to provide the index/keys for the returned array Optional

PHP Example


<?php  
$multi = array( "sid" => array(  "firstname" => "sid", "country" => "united states of america" ),  
"sonoo" => array(  "firstname"=> "sonoo",  "country"=> "london"  )  );  
$arrcol = array_column($multi, 'firstname');  
print_r($arrcol);  
?>  
    

Output

Array( [0] => sid [1] => sonoo )

PHP Example


<?php  
$record = array( array (  'regno' => 101, 'name' => 'sid',  'course' => 'c',  ),  
array(  'regno' => 102,  'name' => 'ajay', 'course' => 'java',  ),  
        array(  'regno' => 103, 'name' => 'rahul',  'course' => 'php',  ) );  
$details = array_column($record, 'course');  
print_r($details);  
?>  
        

Output

Array ( [0] => c [1] => java [2] => php )

PHP Example


<?php  
$record = array(  array(  'id' => 11,  'name' => 'ajay',  'course' => 'C',  ),  
    array( 'id' => 12,  'name' => 'rahul',  'course' => 'PHP',  ),  
    array(  'id' => 13,  'name' => 'abhi',  'course' => 'Java',   ) );  
$details = array_column($record, 'course', 'id');  
print_r($details);  
?>         
        

Output

Array ( [11] => c [12] => php [13] => java )

PHP Example


<?php  
function Column($details)  
{  
$rec = array_column($details, 'hobby');  
return $rec;  
}  
 $details = array( array( 'roll' => 5, 'name' => 'Ashish', 'hobby' => 'Cricket',),  
 array( 'roll' => 1, 'name' => 'Ajay', 'hobby' => 'Football', ),  
array( 'roll' => 3, 'name' => 'Abhishek','hobby' => 'Chess', ),  
                  
);  
print_r(Column($details));  
?>
                
        

Output

Array ( [0] => Cricket [1] => Football [2] => Chess )