php 如何在php中合并两个没有重复值的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8292032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:22:25  来源:igfitidea点击:

How to merge two arrays without duplicate values in php?

phparrays

提问by kamal

I have two arrays: 1. each object here is a row retrived from database.

我有两个数组: 1. 这里的每个对象都是从数据库中检索到的一行。

    array
      1 => 
        object(stdClass)[41]
          public 'id' => string '1' (length=1)
          public 'class_id' => string '25' (length=2)
          public 'section_id' => string '2' (length=1)
          public 'student_id' => string '1' (length=1)
          public 'date' => string '2011-11-27' (length=10)
          public 'attendance' => string 'present' (length=7)
2 => 
        object(stdClass)[41]
          public 'id' => string '1' (length=1)
          public 'class_id' => string '25' (length=2)
          public 'section_id' => string '2' (length=1)
          public 'student_id' => string '3' (length=1)
          public 'date' => string '2011-11-27' (length=10)
          public 'attendance' => string 'present' (length=7)

2. Another array is from my form and this looks like this.

2.另一个数组来自我的表单,它看起来像这样。

array
  0 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 1
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)
  1 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 2
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)

Here what I want to do is:
- compare these two and check if key student_id and date are already on database or not.
- and from second array which is from form data, remove duplicate and insert into data.
The final result should be:

这里我想要做的是:
- 比较这两个并检查关键的 student_id 和 date 是否已经在数据库中。
- 从来自表单数据的第二个数组中,删除重复项并插入数据。
最终结果应该是:

array
  0 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 2
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)

回答by Sudhir Bastakoti

Try:

尝试:

$c = [array_merge][1]($a,$b);

var_dump([array_unique][1]($c));

Hope it helps

希望能帮助到你

回答by user3590539

Complementing the DemoUser's anwer, by setting the flag SORT REGULAR as an argument has resolved my "conversion array to string" problem:

补充 DemoUser 的答案,通过将标志 SORT REGULAR 设置为参数已经解决了我的“将数组转换为字符串”的问题:

$c = array_merge($a,$b);

$d = array_unique($c, SORT_REGULAR);

var_dump($d);

回答by anubhava

Since both your arrays (database and form) are NOTexactly same you cannot call array_merge or array_unique functions. You will need to iterate the database rows once and store in returned values in a separate key-value based array (Map). And then iterate through your form returned array and search the previously prepared array for the key and if found just remove that element from form returned array. Consider following code snippet for this:

由于您的数组(数据库和表单)并不完全相同,因此您不能调用 array_merge 或 array_unique 函数。您将需要迭代数据库行一次并将返回值存储在一个单独的基于键值的数组 (Map) 中。然后遍历表单返回的数组并在先前准备的数组中搜索键,如果找到,则从表单返回的数组中删除该元素。为此考虑以下代码片段:

// assuming database returned rows are in $rows array
// assuming form returned records are in $forms array

$dbArray = array();
foreach($rows as $r) {
   // need to convert string to int and string to date to match data in both sets
   $dbArray[ array( (int) $r->student_id, strtotime($r->date) ) ] = 1;
}

$diffArray = array();
foreach($forms as $f) {
   $key = array( $f['student_id'], strtotime($f['date']) );
   if (!array_key_exists($key, $dbArray))
      $diffArray[] = $f;
}

// now $diffArray will have the final result you're looking for