Check the linkage under the hood. There is a cable that connects the accelerator inside the car to the engine and it pulls back when you press the gas. The cable will run to the engine and attach to a small hook with a spring. It could be spaced incorrectly.
A vacuum leak or a computer issue can cause what you are describing. In park there is no load on the engine and the computer is trying to constantly adjust.
IT sounds like you have a failing idle air control valve. When these go bad they can cause the cars idle to fluctuate. Have a mechanic look at it or test it yourself and see if it needs cleaned.