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

标签 php arrays

我有两个数组: 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。另一个数组来 self 的表单,看起来像这样。

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)

这里我想做的是:
- 比较这两个并检查关键的 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)

最佳答案

试试:

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

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

希望对你有帮助

关于php - 如何在php中合并两个没有重复值的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8292032/

相关文章:

php - 使用 Google Analytics 或自定义解决方案跟踪页面浏览量和事件?

javascript - 声明 JavaScript 数组时 "Array()"和 "[]"有什么区别?

javascript - 如何在 Javascript 中检查每个对象字段的深度

java - 如何将数组的大小加倍

c++ - 为什么大型本地数组会使我的程序崩溃,而全局数组却不会?

php - 比较 laravel 查询结果

php - 使用 ID 数组执行单个数据库查询

php从ids数组中选择多行

php - 为什么 PHP 异常类中的所有方法都是最终的?

javascript - 重写下面一段javascript代码