PHP Programing language

array_intersect() Functions in PHP
Previous Home Next

array_intersect()

The array_intersect() function compares the values of two or more arrays, and returns the match values.

Syntax:
array_intersect(array1,array2,array3...);

In above Syntax,"array1" compare from, "array2" compare against,"array3" compare against to more arrays.

Example:

<?php
$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange","d"=>"Mango");
$array2=array("e"=>"Apple","f"=>"Banana","g"=>"Orange");
$result=array_intersect($array1,$array2);
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Banana [c] => Orange )

 

array_intersect_assoc()

The array_intersect_assoc() function compares the keys and values of two or more arrays, and returns the match values.

Syntax:
array_intersect_assoc(array1,array2,array3...)

In above Syntax,"array1" compare from, "array2" compare against,"array3" compare against to more arrays.

Example:

<?php
$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange","d"=>"Mango");
$array2=array("a"=>"Apple","b"=>"Banana","c"=>"Orange");
$result=array_intersect_assoc($array1,$array2);
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Banana [c] => Orange )

array_intersect_uassoc()

The array_intersect_uassoc() function compares the keys and values of two or more arrays, and returns the match values.

Syntax:
array_intersect_uassoc(array1,array2,array3...,myfunction)

In above Syntax,"array1" compare from, "array2" compare against, "array3" compare against to more arrays and "myfunction" is a string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument

Example:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$array1=array("a"=>"Apple","b"=>"Mango","c"=>"Orange");
$array2=array("d"=>"Apple","b"=>"Mango","e"=>"Orange");
$result=array_intersect_uassoc($array1,$array2,"myfunction");
print_r($result);
?>

Output:

Array ( [b] => Mango )

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: