I have this loop where I need to set a value inside a map (because VF is missing containsKey() function), but once the loop finishes, the value gets reset.
I believe the reason is there are more than one element in your commDetails. There is only one element which satisfies the if statement. But there are more yet to come. The last element doesn't satisfy the if statement and changed the value of p.id key back into 'x'.
In order to prove this, you can put a
System.debug(urlsh) at the end of for loop which is inside the for loop but outside the if statement. You will see the proof.