Update models when there is a join table... best approach?

Second question about join tables: In my app i have a Users and a Groups table. users can be part of one or more groups.

USERS
=====================================
user_id | user_name | member_of_group
=====================================

GROUPS
=====================
group_id | group_name
=====================

'till now no problem in searching and indexing. I’m now at the point i have to UPDATE my models, so I have to also update the join table.

My approach at the moment is: (let’s say I’m updating a USER)
$new_groups=$post[‘goups’] - if isset contains an array of groups, selected in the update form

$old_groups - if isset contains an array of already set groups

foreach($new_groups as $new_group) {
if (!in_array($new_group, $old_groups)) ------> build the model and write it into the table
else remove the new_group from old_groups: they match so i don’t have to write in the table
}
foreach($old_groups as $old_group) { //if any left
delete the row from table
}

this, keeping it as simple as I can, it’s what I’m planning to do. But I’m guessing if is there a more efficient way to do that… or at least a more elegant way…