Why use $tmp at first? Directly filling $mustLocks inside the foreach would make the second iteration disappear.