How to find the closest value in an array

All I’m trying to do is find a value in an array that’s closest to the value of my variable…

So if the value of myVar = 50;
…and the array holds the values 25, 100, 150, etc.

I want to write a function that will be able to find the array element that’s closest to myVar, which would be the first element in the array (myArray[0]), since 25 would be the closest value to 50.

If anyone out there has any ideas, I’d really appreciate it…