You are here

function recipe_ingredients_diff in Recipe 5

Compares two arrays of ingredients and returns the differences

1 call to recipe_ingredients_diff()
recipe_save_ingredients in ./recipe.module
Saves the changed ingredients of a recipe node to the database (by comparing the old and new ingredients first)

File

./recipe.module, line 723
recipe.module - share recipes for drupal 5.x

Code

function recipe_ingredients_diff($a1, $a2) {
  $return->add = array();
  $return->remove = array();
  $return->update = array();
  foreach ($a1 as $pl) {
    $pl = (object) $pl;
    $pl->name = trim($pl->name);
    if ($pl->name) {
      if (!_in_array($pl, $return->add)) {

        // Duplicate entries for the same ingredient are ignored.
        if (!_in_array($pl, $a2)) {
          $return->add[] = $pl;
        }
        else {
          if (!_in_array($pl, $return->update)) {
            $return->update[] = $pl;
          }
        }
      }
    }
  }
  foreach ($a2 as $k => $pl) {
    if (!_in_array($pl, $a1)) {
      $return->remove[] = $pl->id;
    }
  }
  return $return;
}