okay, i'm not going to try and understand the logical aspect of what you're trying to accomplish as i'm not able to visualise your data structures. however, there are some things you can do to speed up your code:
1) for ($i=0; $i<count($existingAssignments); $i++) {
count() is going to be called on every iteration of your loop(s), which would be painfully slow. you should do:
$count = count($array);
for ($i=0; $i<$count; $i++) {
2) i've no idea if this would be any more efficient, but there's no need to have certain evaluations, like you have, nested inside parentheses:
if ( ($a == $b) || ($c == $d) ) {
... this works, but so will:
if ($a == $b || $c == $d) {
3) again, i'm not sure how much faster this would be, but, what about referencing to your $existingAssignments array by number rather than associatively?
4) as for variable assignment, i don't really know how expensive it is - you should try benchmarking it, as well as these other suggestions, and see what works best.
andy.