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

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP array_reverse() Function

PHP array_reverse() Function The array_reverse() function in PHP returns a new array with the elements in the reversed order. Syntax array_reverse (array $array [, bool $preserve_keys = FALSE ] ) Parameter array(required)- This parameter represents the input array preserve_keys (optional)- This parameter specifies if the function should preserve the keys...

1 minute 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 rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP substr_compare() Function

PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] )  Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...

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 array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

1 minute read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute 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_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

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

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

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.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( 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 behavior...

2 minutes read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP ucwords() Function

PHP ucwords() Function The ucwords() function in PHP converts the first character of each word in the specified string to uppercase. It is a binary-safe function. The related function are as follows: ucfirst()lcfirst()strtoupper()strtolower() Syntax ucwords ( string $str [, string $delimiters ] ) Parameter str(required)- This parameter represents...

1 minute read.