Advertisement
cevhyruz

Update List

Jan 23rd, 2022 (edited)
408
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function updateList(array $list) {
  4.   foreach($list['currentList'] as $currentEntry => $currentVal) {
  5.     foreach($list['newList'] as $newEntry => $newVal) {
  6.       // we have a hit
  7.       if ($currentEntry == $newEntry) {
  8.         $list['newList'][$newEntry] += $list['currentList'][$currentEntry];
  9.         continue 2;
  10.       }
  11.       // last entry hit
  12.       if ($newEntry == end(array_keys($list['newList']))) {
  13.         array_push($list['newList'][$currentEntry], $currentEntry);
  14.         $list['newList'][$currentEntry] = $currentVal;
  15.       }
  16.     }
  17.   }
  18.   return $list['newList'];
  19. }
  20.  
  21. // Testing
  22. $InventoryList = array(
  23.   'currentList' => array(
  24.     'Chicken' => 2,
  25.     'Pork' => 3,
  26.     'Ginisa Mix' => 1,
  27.     'Tissue Roll' => 3,
  28.     'Beef' => 5 ),
  29.  
  30.   'newList' => array(
  31.     'Chicken' => 3,
  32.     'Beef' => 2,
  33.     'Beer' => 3,)
  34. );
  35.  
  36. print_r(updateList($InventoryList));
  37.  
  38.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement