Fakedupe
Fakedupe

Reputation: 59

Find intersecting values from multiple arrays

Assuming that you have three arrays which might contain different values as follows.:

$arr1 = array('1', '5', '10');
$arr2 = array('1', '3', '10');
$arr3 = array('1', '6', '10');

How would you strip out what's different and get it as follows?:

$arr1 = array('1', '10');
$arr2 = array('1', '10');
$arr3 = array('1', '10');

I meant I wanted to get it as follows.:

$result = array('1', '10');

Upvotes: 5

Views: 148

Answers (1)

shamittomar
shamittomar

Reputation: 46692

Use array_intersect function:

<?php
$arr1 = array('1', '5', '10');
$arr2 = array('1', '3', '10');
$arr3 = array('1', '6', '10');

$result = array_intersect($arr1, $arr2, $arr3);

print_r($result);

//now assign it back:
$arr1 = $result;
$arr2 = $result;
$arr3 = $result;
?>

Upvotes: 12

Related Questions