Match two arrays values and generate the array of keys whos values are same I manage to solve this as described below but, both array may contain huge records, So can anyone suggest the more optimized solution? result should be same as now
$arr1 = array(
105 => '101,Utility,\N',
102 => '101,Utility,\N',
103 => '105,Parker,Peter',
104 => 'Rahul Ippar'
);
$arr2 = array(
108 => '101,Utility1,Floor',
120 => '101,Utility2,\N',
130 => '105,Parker,Peter',
140 => 'Rahul Ippar'
);
$arr3 = array_intersect( $arr2, $arr1 );
$arr4 = array_flip( $arr1 );
foreach( $arr3 as $k => $v ) {
$arr3[$k] = $arr4[$v];
}
print_r($arr3);
Result should be same as below
Array
(
[130] => 103
[140] => 104
)
An alternative would be:
$result = array_combine(
array_keys(array_intersect( $arr2, $arr1 )),
array_keys(array_intersect( $arr1, $arr2 ))
);
whether it's any faster or more memory efficient, I don't know, though it should be because it's using fewer copies of the data (so less memory usage), and fewer loops; but that's something you'd need to test with your volumes of data