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 usort() Function

PHP usort() Function The usort() function in PHP sorts  array by values using a user-defined comparison function. Syntax usort ( array &$array , callable $value_compare_func ) array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: & OR & (ampersand)" OR " (double quote)'...

2 minutes read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP is_scalar() Function

PHP is_scalar() Function The is_scalar() function in PHP finds whether a variable is a scalar or not. It returns a Boolean value TRUE if the parameter var is scalar, else it returns FALSE. Scalar variables are those containing an integer, float, string or boolean. This...

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.

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...

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 soundex() Function

PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...

1 minute read.

PHP shuffle() Function

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP convert_cyr_string() Function

PHP convert_cyr_string() Function The convert_cyr_string() function in PHP converts from one Cyrillic character-set to another. The supported characters set are given below: k - koi8-rw - windows-1251i - iso8859-5a - x-cp866d - x-cp866m...

1 minute read.

PHP Super Global Variables

There are various predefine “superglobals” in PHP. These variables can easily accessible in function or class. Following are the superglobals variables: $GLOBALS $_SERVER $_REQUEST $_POST $_GETetc. PHP $GLOBALS The “$GLOBAL” isa keyword that...

2 minutes read.

PHP array_diff_key () Function

PHP array_diff_key () Function The array_diff_key () Function in PHP compares the keys from array1 against the keys from arrays. It returns an array containing all the entries from array1 whose keys are absent from all other arrays. This function is like array_diff () except that...

1 minute read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes 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 Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.