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

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP array_sum() Function

PHP array_sum() Function The array_sum() function in PHP is used to calculate the sum of values in an array. Syntax array_sum ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns the sum of values as an...

1 minute read.

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

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP is_bool() Function

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

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

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

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP While Loop

PHP while loop is used to run the same block of code until a condition is met. It should be used if number of iteration it not known. Syntax: <?php while (condition){   //Code...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP is_null() Function

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

1 minute read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

PHP rtrim() Function

PHP rtrim() Function The rtrim() function in PHP is used to strip off the whitespace (or other characters) from the end of a string. If the second parameter is not specified, this function will strip are...

1 minute read.