- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
array_intersect() function in PHP
The array_intersect() function compares array values, and returns the matches. It returns an array containing all of the values in the first array whose values exist in all of the parameters.
Syntax
array_intersect(arr1, arr2, arr3, arr4, …)
Parameters
arr1 − Array to compare from. Required.
arr2 − Array to compare against. Required.
arr3 − You can add more arrays to compare. Optional.
arr4 − You can add more arrays to compare. Optional.
Return
The array_intersect() function returns an array containing all of the values in the first array whose values exist in all of the parameters.
Example
<?php $a1 = array("p"=>"Windows","q"=>"Mac","r"=>"Linux"); $a2 = array("s"=>"Windows","t"=>"Linux"); $result = array_intersect($a1,$a2); print_r($result); ?>
Output
Array ( [p] => Windows [q] => Linux )
Let us see another example.
Example
<?php $arr1 = array(15, 30, 40, 60, 78, 100, 130, 145, 150); $arr2 = array(50, 60, 70, 80, 90, 100); $res = array_intersect($arr1,$arr2); print_r($res); ?>
Output
Array ( [3] => 60 [5] => 100 )
Advertisements
To Continue Learning Please Login
Login with Google