PHP | array_column() Function
Last Updated :
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.
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_column
function 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_column
function 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
)
Similar Reads
PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
2 min read
PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
2 min read
PHP array() Function The array() function is an inbuilt function in PHP which is used to create an array. There are three types of array in PHP: Indexed array: The array which contains numeric index. Syntax: array( val1, val2, val3, ... ) Associative array: The array which contains name as keys. Syntax: array( key=>v
2 min read
PHP | ArrayObject count() Function The ArrayObject::count() function is an inbuilt function in PHP which is used to get the number of public properties in the ArrayObject. Syntax: int ArrayObject::count( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the number of public propertie
1 min read
PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read
PHP Array Functions Arrays are one of the fundamental data structures in PHP. They are widely used to store multiple values in a single variable and can store different types of data, such as strings, integers, and even other arrays. PHP offers a large set of built-in functions to perform various operations on arrays.
7 min read
PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
2 min read
PHP array_filter() Function This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback func
2 min read
PHP | is_array() Function The is_array() is an inbuilt function in PHP. The is_array() function is used to check whether a variable is an array or not. Syntax: bool is_array($variable_name) Parameter: This function accept a single parameter as mentioned above and described below: $variable_name: This parameter holds the vari
2 min read
PHP in_array() Function The in_array() function in PHP is a built-in function that is used to check if a specific value exists within an array and returns a boolean result. Returns true if the value is found and false if the value is not found.Syntax:bool in_array(mixed $needle, array $haystack, bool $strict = false)In thi
3 min read