I want to compare the data in whole rows between the two arrays and produce:
- an array of intersecting rows and
- an array where rows in the first array are not found in the second array and
- an array where rows in the second array are not found in the first array.
I have two multidimensional arrays with me.
$array1 = [
['sight_id' => 13, 'location' => 'Jodhpur, Rajasthan, India'],
['sight_id' => 14, 'location' => 'Jodhpur Jn, Jodhpur, Rajasthan, India'],
['sight_id' => 15, 'location' => 'D-Kirtinagar, Jodhpur, Rajasthan, India'],
];
$array2 = [
['sight_id' => 13, 'location' => 'Jodhpur, Rajasthan, India'],
['sight_id' => 14, 'location' => 'Jodhpur Jn, Jodhpur, Rajasthan, India'],
['sight_id' => 16, 'location' => 'Jaisalmer, Rajasthan, India'],
['sight_id' => 17, 'location' => 'Fort Road, Amar Sagar Pol, Jaisalmer, Rajasthan, India'],
];
Desired results:
$intersect_array = [
['sight_id' => 13, 'location' => 'Jodhpur, Rajasthan, India'],
['sight_id' => 14, 'location' => 'Jodhpur Jn, Jodhpur, Rajasthan, India'],
and
$only_a1 = [
['sight_id' => 15, 'location' => 'D-Kirtinagar, Jodhpur, Rajasthan, India'],
]
and
$only_a2 = [
['sight_id' => 16, 'location' => 'Jaisalmer, Rajasthan, India'],
['sight_id' => 17, 'location' => 'Fort Road, Amar Sagar Pol, Jaisalmer, Rajasthan, India'],
];
function multi_array_intersect($a, $b) { return strcmp ( $a ['sight_id'], $b ['sight_id'] ); } $common_sights = array_uintersect ( $a1, $a2, 'multi_array_intersect' );