How to use array intersect in PHP
In this article, I will explain how the array() function can be used in PHP.
array_intersect() function in PHP
- array_intersect() function compares two or more arrays, if the value is present in all of the other arrays, it returns an array with the values and keys from the first array.
- In array_intersect() function, only the value is used in the comparison.
Syntax
array_intersect(array1,array2,array3....) |
Parameter
-
array1 array1 is required parameter. The first array is the array which will be compared with other arrays.
-
array2 array2 is required parameter. An array to be compared with the first array.
-
array3 array3 is optional parameter. An array to be compared with the first array.
Example
The following example show to how the array_intersect() function can be used in PHP.
<html>
<body>
<h3 style="color: darkolivegreen;">array_intersect() function example in PHP</h3>
<?php
$a1=array(0=>"student",1=>"Teacher",2=>"Worker");
$a2=array(3=>"Teacher",4=>"Worker",5=>"Advocate");
print_r(array_intersect($a1,$a2));
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here