PHP array_intersect_key() function

The array_intersect_key () function in PHP is used to compute the intersection of arrays using keys for comparison. It returns an array containing all the entries from array1 which have keys that are present in all the other arrays.

Syntax

array_intersect_key (array ;$array1, array $array2 [array;$...])

Parameter

array1(required)- This parameter signifies the input array to compare from.

array2(required)- This parameter signifies the input array to compare against.

array3….(optional)- It represents more arrays to compare against

Return

This function returns an array containing all the values in array1 which have keys that are present in all the other arrays.

Example 1

"F");
echo("\nArray 2: \n");
var_export($array2);
//returning the intersection of array1 and array2 based on their keys
$result_array = array_intersect_key($array1, $array2);
echo("\nIntersection: \n");
//printing the intersection of the keys
print_r($result_array);
?> 

Output

Array 1: array (
0 => 'A',
1 => 'b',
2 => 'c',
3 => 'd',
)
Array 2: 
array (
  0 => 'A',
  1 => 'd',
  2 => 'A',
  5 => 'F',
)
 Intersection: 
 Array
 (
     [0] => A
     [1] => b
     [2] => c
 ) 

Example 2

 "Reema", 12 => "Akash", 3 => "Sukla", 4 >"Varun",5=>"Mukta",6=>"Amar");
//initializing array2
$array2 = array(1 => "Reema", 2 => "Akash", 3 => "Tisha", 4 =>"Varun");
//initializing array3
$array3 = array(11 => "Rahul", 2 => "Akash", 3 => "Tisha", 4 =>"Varun",5=>"Reema");
//computing the intersection of the keys
$intersection = array_intersect_key($array1,$array2,$array3);
//printing the intersection
print_r($intersection);
?> 

Output

Array
(
 [3] => Sukla
 [4] => Varun

) 

Example 3

1,2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9,10=>10);
//initializing array2 with odd number
$array2 = array(1=>1,3=>3,5=>5,7=>7,9=>9);
//computing the intersection of the keys
$intersection = array_intersect_key($array1,$array2);
//printing the intersection i.e., odd numbers
print_r($intersection);
?> 

Output

Array
(
[1] => 1
[3] => 3
[5] => 5
[7] => 71
[9] => 9
) 

Related Topics

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

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

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

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

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

PHP strcspn() Function

PHP strcspn() Function The strcspn() function in PHP returns the number of characters (including whitespaces) found in a string before any part of the specified characters is found. It is a binary-safe function. Syntax strcspn ( string $subject , string $mask [, int $start [, int $length ]] ) Parameter subject(required)- This...

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

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP natsort() Function

PHP natsort() Function The natsort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natsort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean value TRUE on success or FALSE on...

1 minute read.