foreach loop inside the loop creating duplicates in output

This is a php logic / variable issue. You output $fullname whether or not it has been set inside that iteration’s foreach. So for projects that never enter the foreach to set $fullname, it still contains whatever the last iteration had set it to. Set it to an empty string before the foreach so it only contains a name if it enters the foreach.