title: PHP find value in the range between the values of an array, and then save the key
body: <p>Good morning from Itlay,</p>
  <p>I have a problem with an array and creating a function to check if a value is in a range between two values of array elements, and then (if so) return the key of the first one (and saving in a $var).</p>
  <p>On the one hand, the array will have this structure:</p>
  <pre><code>array (
  RED =&gt; 10;
  YELLOW =&gt; 50;
  GREEN =&gt; 80;
  BLACK =&gt; 110;
  ...
  )
  </code></pre>
  <p>And on the other hand, I will have a foreach loop where I need to check each time if a value is in the range where: (i) the minimum is the value of the array element; and (ii) the maximum is the value of the next element.
  So run among all the array items and then, when it is in the range, setting a $variable with the key of the array element.</p>
  <p>CLARIFYING:</p>
  <p>Let's take an example with a value of 55 in the foreach loop.
  The function must:</p>
  <ol>
  <li>first check if 55 is between the first and second element (and so between [RED]=10 and [YELOW]=50);</li>
  <li>then - the answer being no - check if 55 is between the second and third element (and so between [YELLOW]=50 and [GREEN]=80); and so on...</li>
  <li>then - when the answer is yes - setting the variable $var equal to the key of the first item of the correct comparison (and so: $var = YELLOW).</li>
  </ol>
  <p><strong>EDIT 1</strong></p>
  <p>Thanks to Mick, I have developed the following function. Still does not work.</p>
  <pre><code>&lt;?php
  function keybetween($array, $number) {
      $b = 0;
      foreach ($array as $a) {
          if ($number&gt; $a &amp;&amp; $number &lt; $b){ 
            return $a; // How I can return the key instead of the value?
            break;
          }else{
            $b = $a;
          }
      }
      return end($array); // or return NULL;
  }
  ?&gt;
  </code></pre>
  <p>Do you see any error? And how can I return the key instead of the value?</p>