There’s a phenomenon called ‘osmosis’ which plays its role here.
When two different liquids with different concentrations are separated by a semipermeable membrane (the one that allows one-way flow only), the liquids would flow in such a way that they attain a state of equilibrium, i.e. now the concentration of both the liquids is same.
Salty food doesn’t help the way salt water does for several reasons.
For one, there isn’t enough of the right kind of salt in most salty foods to make your throat less bacteria-friendly.
Secondly, there are enough carbs and sugars in most salty foods to feed the bacteria making your throat sore. This is obviously counter-productive.
Thirdly, salty foods usually have a very rough texture which will scrape and irritate your throat, which is bad by itself but the super tiny scrapes also make great hiding places for the bacteria to grow.
So gargling salt water is a great idea and will help kill the bacteria making your throat sore. Eating salty food is a terrible idea because it basically builds a cozy place for all the bacteria to live and then gives them a bunch of free food.