比较两个多维数组然后创建唯一的数组

时间:2022-04-10 08:51:02

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.

我需要比较$ newData和$ oldData并仅获取第一个公共数组之前的新数据。

My $newData will then be:

我的$ newData将是:

$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.

我已尝试过array_unique中的所有内容,如果比较ID键但没有任何工作正常。

Do I need to merge them, first? map them? Bahh, I have no idea, I am so lost.

我需要首先将它们合并吗?映射他们?呸,我不知道,我迷失了。

Any help would be awesome

任何帮助都是极好的

3 个解决方案

#1


7  

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

我只是做一个嵌套的foreach循环。我不知道你正在使用哪种编程语言,但假设它是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.

然后在$ tmpArray变量中有所需的数组。你当然可以制作$ newData = $ tmpArray;之后。

#2


1  

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

只需迭代$ newData,直到找到$ 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.

您的新数据将存储在$ data中。

#3


0  

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

用这个怎么样?通过修改lamborghinistory的答案,

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

#1


7  

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

我只是做一个嵌套的foreach循环。我不知道你正在使用哪种编程语言,但假设它是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.

然后在$ tmpArray变量中有所需的数组。你当然可以制作$ newData = $ tmpArray;之后。

#2


1  

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

只需迭代$ newData,直到找到$ 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.

您的新数据将存储在$ data中。

#3


0  

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

用这个怎么样?通过修改lamborghinistory的答案,

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