Compare two multidimensional arrays then create array of only unique

arrays, multidimensional-array, php, unique

Solution

I would just do a nested foreach loop. I don't know which programming language You're using, but assuming that it's PHP ($):

$tmpArray = array();

foreach($newData as $data1) {

  $duplicate = false;
  foreach($oldData as $data2) {
    if($data1['id'] === $data2['id'] && $data1['name'] === $data2['name'] && $data1['sex'] === $data2['sex']) $duplicate = true;
  }

  if($duplicate === false) $tmpArray[] = $data1;
}

You then have the desired array in the `$tmpArray` variable. You can make of course `$newData = $tmpArray;` afterwards.

Problem

I've been trying to get this for hours now. I have two multidimensional arrays ``` $newData ( [0] => Array( [id] => 1 [name] => John [sex] => male ) [1] => Array( [id] => 2 [name] => Kenny [sex] => male ) [2] => Array( [id] => 3 [name] => Sarah [sex] => female ) [3] => Array( [id] => 4 [name] => George [sex] => male ) ) $oldData ( [0] => Array( [id] => 3 [name] => Sarah [sex] => female ) [1] => Array( [id] => 4 [name] => George [sex] => male ) [2] => Array( [id] => 5 [name] => Peter [sex] => male ) [3] => Array( [id] => 6 [name] => Lexi [sex] => female ) ) ``` I need to compare $newData and $oldData and grab only the new data that is before the first common array. My $newData will then be: ``` $newData ( [0] => Array( [id] => 1 [name] => John [sex] => male ) [1] => Array( [id] => 2 [name] => Kenny [sex] => male ) ``` ) I've tried everything from array_unique, if comparing the ID keys but nothing is working properly. Do I need to merge them, first? map them? Bahh, I have no idea, I am so lost. Any help would be awesome

Original source