array_intersect(): Intersection of two or more arrays

This function takes two or more arrays as input and then returns the associative array containing all the values in input array ( $array1) that are present in all of the arguments. ( keys are not considered for comparison )
$new_array = array_intersect ($array1, $array2,....);
$array1 : Required , the input array which will be checked.
$array2 : Required , the values to be matched with $array1.

Here values are used for comparison ( Keys are not considered ). ( In array_intersect_assoc() Both values and keys are considered for comparison )

Example 1

$first=array(1,2,3,4);
$second=array(1,2,3,5);
$result1=array_intersect($first,$second);
while (list ($key, $val) = each ($result1)) {
echo "$key -> $val <br>";
}
Output is here.
0 -> 1 
1 -> 2 
2 -> 3 
Keys of the first index are retained. ( no re-indexing done here) .

Example 2

$first=array('One' =>1,'Two'=>2,'Three'=>3,'Four'=>'Fourth');
$second=array('One'=>1,'Two'=>2,'Third'=>3);

$result1=array_intersect_assoc($first,$second);
while (list ($key, $val) = each ($result1)) {
echo "$key -> $val <br>";
}
Output is here.
One -> 1 
Two -> 2 
Three -> 3 


plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer