Reputation: 2706
I have two array. I want to get the common value which have both aray indexing value is same.
Check my code-
$arr1 = array(0=>5,1=>7, 2=>9, 3=>4, 4=>2, 5=>8, 6=>7, 7=>0, 8=>1);
$arr2 = array(0=>7,1=>2, 2=>4, 3=>5, 4=>9, 5=>8, 6=>5, 7=>0, 8=>6);
I want to like-
array(
5=>8
7=>0
)
And there is $arr2
is a duplicate value name 5
i also want to get duplicate value from $arr2
array(
3=>5
6=>5
)
How to get the matching value and duplicate value of above two Array?
Upvotes: 0
Views: 71
Reputation: 427
this code works fine
<?php
$arr1 = array(0=>5,1=>7, 2=>9, 3=>4, 4=>2, 5=>8, 6=>7, 7=>0, 8=>1);
$arr2 = array(0=>7,1=>2, 2=>4, 3=>5, 4=>9, 5=>8, 6=>5, 7=>0, 8=>6);
$arr = array_intersect_assoc($arr1, $arr2);
print_r($arr);
$duplicate=array(); $duplicateVirtual=[];
foreach($arr2 as $index=>$val)
{
if(in_array($val,$duplicateVirtual)){ $duplicate[$index]=$val; }
$duplicateVirtual[]=$val;
}
print_r($duplicate);
You can even create a function to check for duplicate and pass array as variable.
Upvotes: 0
Reputation: 219884
You are looking for array_intersect_assoc()
$arr1 = array(0=>5,1=>7, 2=>9, 3=>4, 4=>2, 5=>8, 6=>7, 7=>0, 8=>1);
$arr2 = array(0=>7,1=>2, 2=>4, 3=>5, 4=>9, 5=>8, 6=>5, 7=>0, 8=>6);
$arr = array_intersect_assoc($arr1, $arr2);
Upvotes: 1