PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays.

Syntax

array_intersect ( 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 whose values are present in all the other arrays.

Example 1

"F");
echo("\nArray 2: \n");
var_export($array2);
//returning the intersection of array1 and array2
$result_array = array_intersect($array1, $array2);
echo("\nIntersection: \n");
//printing the intersection of the array based on values
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
    [3] => d
) 

Example 2

 

Output

Array 1: 
array (
  0 => 'aqua',
  1 => 'green',
  2 => 'red',
  3 => 'blue',
  4 => 'red',
)
Array 2: 
array (
  0 => 'blue',
  1 => 'green',
  2 => 'yellow',
  3 => 'blue',
)
 Intersection: 
Array
(
    [1] => green
    [3] => blue
) 

Example 3

 "Reema", 2 => "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(1 => "Rahul", 2 => "Akash", 3 => "Tisha", 4 =>"Varun");
//computing the intersection between array1, array2 and array3
$intersection = array_intersect($array1,$array2,$array3);
//printing the intersection containing all the entries from array1 that are present in array2 nd array3
print_r($intersection);
?> 

Output

Array
(
    [2] => Akash
    [4] => Varun
) 

Related Topics

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

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

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

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

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes 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 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 array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

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

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes 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 chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.