I'm sure this has been addressed before but i didn't see it in a search.

Let's say I have an array and a variable whose value exists somewhere in the array.

I want to locate the variable in the array and determine the value immediately prior to it and immediately after it.

How do I do that efficiently?

    Use array_search to find the key of the array where the variable is stored.
    Then determine if this key is at the beginning or end of the array then you can determine the values of the array before and after using key+1 and key-1

    Keep in mind though that you will get errors trying to look at the value of Key = -1 and the value of Key > ArrayLength

      Thank you, FijiSmithy.
      That was exactly what I needed.

        Write a Reply...