21

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)
kamal
  • 1,536
  • 2
  • 22
  • 32

3 Answers3

26

Try:

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

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

Hope it helps

Larzan
  • 9,389
  • 3
  • 42
  • 41
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
7

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

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

$d = array_unique($c, SORT_REGULAR);

var_dump($d);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
user3590539
  • 71
  • 1
  • 1
2

Since both your arrays (database and form) are NOT exactly 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:

// 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
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • it gives error: Severity: Warning Message: Illegal offset type on $dbArray[ array( (int) $r->student_id, strtotime($r->date) ) ] = 1; – kamal Nov 29 '11 at 07:21