8

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

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

by copying your code, then changing the key names to what I currently have, it returns an empty array when I print_r $tmpArray
then You obviously mistyped something. If I try this code here pastebin.com/jJgDvLxE I get exactly the array which You wanted as a result...
I might of, but I just rebuilt the code so if was easier to read for me. regardless, it works, and you just saved me from pulling all my hair out. I was way way WAY overthinking this.
1

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.

Comments

0

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

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.