且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-19 14:01:14

尝试:


$ C = [array_merge] [1]($ A,$ B);后续代码var_dump([array_unique] [1]($ C));

希望它能帮助

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

    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.

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:

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)

Try:

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

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

Hope it helps