Ryan
Ryan

Reputation: 2234

Compare two multidimensional arrays then create array of only unique

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

Upvotes: 8

Views: 10362

Answers (3)

Keyur
Keyur

Reputation: 1111

How about using this? By modifying thelamborghinistory's answer,

Unset the dynamic key if there is a duplicate.

public function removeDuplicate($oldData, $newData, $key)
{
    $tmpArray = array();
    foreach ($newData as $data1) {
        $duplicate = false;
        foreach ($oldData as $data2k => $data2v) {
            if ($data1[$key] === $data2v[$key]) {
                unset($newData[$data2k]);
                $duplicate = true;
            }
        }
        if ($duplicate === true) {
            $tmpArray[] = $data1;
        } else {
            $tmpArray[] = $data1;
        }
    }
    return $tmpArray;
}

And then you have to just call,

removeDuplicate($oldData, $newData, 'name');//or whatever you want

Upvotes: 0

Maxim Zubarev
Maxim Zubarev

Reputation: 2473

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.

Upvotes: 8

Michal Rus
Michal Rus

Reputation: 1848

Just iterate over $newData until you find the first element from $oldData?

$cmp = $oldData[0];
$data = array();
foreach ($newData as $el) {
    if ($el['id'] === $cmp['id']
      && $el['name'] === $cmp['name']
      && $el['sex'] === $cmp['sex'])
        break;
    $data[] = $el;
}

Your new data will be stored in $data.

Upvotes: 1

Related Questions