-1

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'],
];
2
  • For common data array I done this..function multi_array_intersect($a, $b) { return strcmp ( $a ['sight_id'], $b ['sight_id'] ); } $common_sights = array_uintersect ( $a1, $a2, 'multi_array_intersect' ); Commented Jun 1, 2016 at 9:49
  • Edit your question to explain what have you done to try achieving desired result. Commented Jun 1, 2016 at 10:51

2 Answers 2

1

Convert arrays to a format, where array index is the sight_id:

$b1 =array();
foreach($a1 as $x)
    $b1[$x['sight_id']] = $x['location'];

$b2 =array();
foreach($a2 as $x)
    $b2[$x['sight_id']] = $x['location'];

Calculate the differences and intersection:

$c_intersect = array_intersect_key($b1,$b2);
$c_1 = array_diff_key($b1,$b2);
$c_2 = array_diff_key($b2,$b1);

Convert arrays back to your format:

$intersect_array = array();
foreach($c_intersect as $i=>$v)
    $intersect_array[] = array('sight_id'=>$i,'location'=>$v);

$only_a1 = array();
foreach($c_1 as $i=>$v)
    $only_a1[] = array('sight_id'=>$i,'location'=>$v);

$only_a2 = array();
foreach($c_2 as $i=>$v)
    $only_a2[] = array('sight_id'=>$i,'location'=>$v);
Sign up to request clarification or add additional context in comments.

2 Comments

What if in future I have to add two more columns to that array..??
Then instead of storing just $x['location'] column, store whole row $x . Then restore it simply $only_a2[] = $v
1

It is not necessary to mutate/prepare your two-dimensional arrays in order to compare the rows. Because whole rows from one array relate to whole rows in the other array, you don't need to specify a column to compare -- just compare the full row data.

Code: (Demo) (Or with a declared function)

echo "Intersection:\n";
var_export(
    array_uintersect($array1, $array2, fn($a, $b) => $a <=> $b)
);

echo "\nUnique in first:\n";
var_export(
    array_udiff($array1, $array2, fn($a, $b) => $a <=> $b)
);

echo "\nUnique in second:\n";
var_export(
    array_udiff($array2, $array1, fn($a, $b) => $a <=> $b)
);

Output:

Intersection:
array (
  0 => 
  array (
    'sight_id' => 13,
    'location' => 'Jodhpur, Rajasthan, India',
  ),
  1 => 
  array (
    'sight_id' => 14,
    'location' => 'Jodhpur Jn, Jodhpur, Rajasthan, India',
  ),
)
Unique in first:
array (
  2 => 
  array (
    'sight_id' => 15,
    'location' => 'D-Kirtinagar, Jodhpur, Rajasthan, India',
  ),
)
Unique in second:
array (
  2 => 
  array (
    'sight_id' => 16,
    'location' => 'Jaisalmer, Rajasthan, India',
  ),
  3 => 
  array (
    'sight_id' => 17,
    'location' => 'Fort Road, Amar Sagar Pol, Jaisalmer, Rajasthan, India',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.