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

kam*_*mal 17 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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

Sud*_*oti 26

尝试:

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

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

希望能帮助到你


小智 5

通过将标志SORT REGULAR设置为参数来补充DemoUser的anwer,解决了我的"转换数组到字符串"问题:

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

$d = array_unique($c, SORT_REGULAR);

var_dump($d);
Run Code Online (Sandbox Code Playgroud)