PHP | array_column() Function - GeeksforGeeks

29 Sep, 2021
The array_column() is an inbuilt function in PHP and is used to return the values from a single column in the input array. 
Syntax: 
array array_column($input_array, $column_number, $index_key);
Parameters: Out of the three parameters, two are mandatory and one is optional. Let’s look at the parameters. 
$input_array (mandatory): This parameter refers to the original multi-dimensional array from which we want to extract all the values of a particular column.$column_number (mandatory): This parameter refers to the column of values that is needed to be returned. This value may be an integer key of the column, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects.$index_key (optional): This is an optional parameter and refers to the column to use as the index/keys for the returned array in output. This value may be the integer key of the column, or it may be the string key name.
$input_array (mandatory): This parameter refers to the original multi-dimensional array from which we want to extract all the values of a particular column.
$column_number (mandatory): This parameter refers to the column of values that is needed to be returned. This value may be an integer key of the column, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects.
$index_key (optional): This is an optional parameter and refers to the column to use as the index/keys for the returned array in output. This value may be the integer key of the column, or it may be the string key name.
Return Type: As shown in the syntax, the return type of array_column() function is array. That is, the function returns an array that contains values from a single column of the input array, identified by a column_number. Optionally, an index_key may also be provided to index the values in the returned array by the values from the index_key column of the input array.
Examples: 
Input : 
      array(
        array(
            'roll' => 5,
            'name' => 'Akash',
            'hobby' => 'Cricket',
        ),
        array(
            'roll' => 1,
            'name' => 'Rishav',
            'hobby' => 'Football',
        ),
        array(
            'roll' => 3,
            'name' => 'Anand',
            'hobby' => 'Chess',
        ),
      )
  
      $column_number = 'hobby'  ,   $index_key = 'roll'
Output : 
      Array
      (
         [5] => Cricket
         [1] => Football
         [3] => Chess
         [4] => Cards
         [2] => Basketball
      )
In the above example, the array_column() function is used to fetch the values of column with key as ‘name’ and these values in the output array are stored with keys which are taken from values of the key ‘roll’ in the original array.
Below program illustrates the array_column() with all three parameters:  
C++
<?php// PHP code to illustrate the working of array_columnfunction Column($details){    $rec = array_column($details, 'name', 'roll');    return $rec;} // Driver Code$details = array(    array(        'roll' => 5,        'name' => 'Akash',        'hobby' => 'Cricket',    ),    array(        'roll' => 1,        'name' => 'Rishav',        'hobby' => 'Football',    ),    array(        'roll' => 3,        'name' => 'Anand',        'hobby' => 'Chess',    ),    array(        'roll' => 4,        'name' => 'Gaurav',        'hobby' => 'Cards',    ),    array(        'roll' => 2,        'name' => 'Rahim',        'hobby' => 'Basketball',    ),);print_r(Column($details));?>
Output: 
Array
(
    [5] => Akash
    [1] => Rishav
    [3] => Anand
    [4] => Gaurav
    [2] => Rahim
)
We can also ignore the third parameter that is index_key. Then, in this case, the column in output array will get indexed in a linear manner as given in the array. Below is the PHP program to illustrate this:  
C++
<?php// PHP code to illustrate the working of array_columnfunction Column($details){    $rec = array_column($details, 'hobby');    return $rec;} // Driver Code$details = array(    array(        'roll' => 5,        'name' => 'Akash',        'hobby' => 'Cricket',    ),    array(        'roll' => 1,        'name' => 'Rishav',        'hobby' => 'Football',    ),    array(        'roll' => 3,        'name' => 'Anand',        'hobby' => 'Chess',    ),    array(        'roll' => 4,        'name' => 'Gaurav',        'hobby' => 'Cards',    ),    array(        'roll' => 2,        'name' => 'Rahim',        'hobby' => 'Basketball',    ),);print_r(Column($details));?>
Output: 
Array
(
    [0] => Cricket
    [1] => Football
    [2] => Chess
    [3] => Cards
    [4] => Basketball
)
 
simranarora5sos
PHP-array
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to create footer to stay at the bottom of a Web page?
How to set the default value for an HTML <select> element ?