Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two arrays without duplicate values in php?

Tags:

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) 
like image 611
kamal Avatar asked Nov 28 '11 05:11

kamal


1 Answers

Try:

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

Hope it helps

like image 148
Sudhir Bastakoti Avatar answered Sep 20 '22 20:09

Sudhir Bastakoti