Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 16th, 2018 71 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. $old_data = [
  2.     ['name'  => 'name1', 'value' => '123'],
  3.     ['name'  => 'name2', 'value' => '456'],
  4.     ['name'  => 'name3', 'value' => '789'],
  5. ];
  6.    
  7. $new_data = [
  8.     ['name'  => 'name1', 'value' => '321'], // value изменился
  9.     ['name'  => 'name2', 'value' => '456'],
  10.     ['name'  => 'name3', 'value' => '789'],
  11.     ['name'  => 'name4', 'value' => '999'], // новый элемент
  12.     ['name'  => 'name5', 'value' => '000'], // новый элемент
  13. ];
  14.    
  15. foreach ($new_data as $key => $value) {
  16.     if(isset($old_data[$key])){
  17.         $diff = array_diff($value, $old_data[$key]);
  18.     } else {
  19.         /// тут обрабатываем новые элементы, например добавляем их в результирующий массив diff
  20.     }
  21. }
  22.    
  23. $diff = array_filter($new_data, function ($element) use (&$old_data) {
  24.     return !in_array($element, $old_data);
  25. });
  26. var_dump($diff);
  27.    
  28. $diff = array_udiff( $old_data, $new_data, function ($a, $b) { return $a != $b; } );
  29. var_dump($diff);
  30.    
  31. //сравниваем имеющиеся и новые массивы записей, к примеру, на основе поля 'url':
  32. $to_insert = [];
  33. $to_delete = [];
  34. $len_new = count($new_records);
  35. $len_last = count($last_records);
  36. for ($i=0; $i < $len_new; $i++) {
  37.     $to_insert_counter = 0;
  38.     for ($j=0; $j < $len_last; $j++) {
  39.         if ($new_records[$i]['url'] !== $last_records[$j]['url']) {
  40.             $to_insert_counter++;
  41.             if ($to_insert_counter === $len_last)
  42.                 $to_insert[] = $new_records[$i];
  43.             $to_delete_counter++;
  44.             if ($to_delete_counter === $len_new)
  45.                 $to_delete[] = $last_records[$j];
  46.         }
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top