PHP array_column() Function

PHP array_column() Function

The array_column() function in PHP is used to return the values from a single column specified in the input array.

Syntax

array_column (array $input, mixed $column_key [, mixed $index_key])

Parameter

input(required)- This parameter represents a multi-dimensional array or an array of objects from which the column of values will be pulled from.

column_key(required)- This parameter signifies 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. 

index_key(optional)- It signifies the column to use as the index/keys for the returned array. The default value is null.

Return

This function returns an array of values representing a single column from the input array.

Example 1

 '15cs1029',
         'first_name' => 'Reema',
         'last_name' => 'Panda',
     ), 
     array(
         'id' => '15cs1011',
         'first_name' => 'Arnavi',
         'last_name' => 'Patra',
          )
 ); 
 // will return the value for the first name column.
 $first_names = array_column($records, 'first_name');
 echo("The First Names of the students are as follows:\n");
 print_r($first_names);
 // will return the value for the last name column.
 $last_names = array_column($records, 'last_name');
 echo("\nThe Last Names of the students are as follows:\n");
 print_r($last_names);
 ?> 

Output

The First Names of the students are as follows:
 Array
 (
     [0] => Reema
     [1] => Arnavi
 )
 The Last Names of the students are as follows:
 Array
 ( 
     [0] => Panda
     [1] => Patra
 ) 

Example 2

 '15cs1029',
         'first_name' => 'Reema',
         'last_name' => 'Panda',
     ), 
     array(
         'roll_no' => '15cs1011',
         'first_name' => 'Arnavi',
         'last_name' => 'Patra',
          )
 ); 
 //will return roll_no as keys and first_name as values
 $values = array_column($records, 'first_name', 'roll_no');
 print_r($values);
 ?> 

Output

Array
 (
     [15cs1029] => Reema
     [15cs1011] => Arnavi
 ) 

Example 3

 array(  "Nickname" => "Reem", "country" => "India","Nationality"=>"Indian" ),  
  "Varun" => array(  "Nickname" => "Garo", "country" => "India","Nationality"=>"Indian" )  );  
 $nickname = array_column($arr, 'Nickname');  
 // will return the values for column nickname
 print_r($nickname);  
 ?>   

Output

Array
 (
     [0] => Reem
     [1] => Garo
 ) 

Example 4

 array(  "Nickname" => "Reem", "country" => "India","Nationality"=>"Indian" ),  
  "Varun" => array(  "Nickname" => "Garo", "country" => "India","Nationality"=>"Indian" )  );  
 $nickname = array_column($arr, 'last_name');  
 // will return the null as the last_name column is not present in the array
 print_r($nickname);  
 ?>   

Output

Array
 (
 ) 

Related Topics

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP strchr() Function

PHP strchr() Function The strchr() function in PHP searches for the first occurrence of the specified string inside another string. It is a binary-safe case -sensitive function and is the alias of strstr() function. Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] ) Parameter haystack(required)-...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP strstr() Function

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

2 minutes read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP array_walk_recursive() Function

PHP array_walk_recursive() Function The array_walk_recursive () function in PHP is used to apply a user-defined callback function recursively to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents the...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP unserialize() Function

PHP unserialize() Function The unserialize() function in PHP takes a single serialized variable and converts it back into a PHP value. Syntax unserialize ( string $str [, array $options ] ) Parameter value(required)- This parameter represents the serialized string. options(optional)- This parameter signifies any options to be...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.

PHP is_countable() Function

The PHP is_countable() function in PHP is used to  find whether a variable is countable or not. It returns a Boolean value true if the parameter var is countable else it returns false. Syntax is_countable ( mixed...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the...

1 minute read.

PHP strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.