0

Good morning from Itlay,

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).

On the one hand, the array will have this structure:

array (
RED => 10;
YELLOW => 50;
GREEN => 80;
BLACK => 110;
...
)

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.

CLARIFYING:

Let's take an example with a value of 55 in the foreach loop. The function must:

  1. first check if 55 is between the first and second element (and so between [RED]=10 and [YELOW]=50);
  2. 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...
  3. 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).

EDIT 1

Thanks to Mick, I have developed the following function. Still does not work.

<?php
function keybetween($array, $number) {
    $b = 0;
    foreach ($array as $a) {
        if ($number> $a && $number < $b){ 
          return $a; // How I can return the key instead of the value?
          break;
        }else{
          $b = $a;
        }
    }
    return end($array); // or return NULL;
}
?>

Do you see any error? And how can I return the key instead of the value?

Andreito
  • 11
  • 3
  • 1
    Please share with us your best effort (code). You might be closer to a solution than you think. Do you get any specific error(s)? Read [ask] – berend Apr 30 '21 at 07:30
  • 1
    https://stackoverflow.com/q/24529819/2943403 – mickmackusa Apr 30 '21 at 07:32
  • No need for filtering, a straight up loop like the one in the question @mickmackusa linked to will do. – El_Vanja Apr 30 '21 at 07:38
  • and https://stackoverflow.com/q/6147356/2943403 (you would need to amend the snippet to return the key instead of the value though) Do not use `array_filter()` if you only seeking the first qualifier -- a `foreach()` can be short circuited with `break` but `array_filter()` cannot. – mickmackusa Apr 30 '21 at 07:42
  • I don't understand why the question has been closed. The function in the post indicated is related to "the closest number" and is very different from the goal I'm looking for. Even the link indicated by @mickmackusa (which I thank a lot) is quite different! I try to edit the question in 5 minute. Let's look if together we can fix the problem. – Andreito Apr 30 '21 at 08:08
  • @mickmackusa I edited the question with a solution I'm trying. Help? – Andreito Apr 30 '21 at 08:18
  • a quick [stab](https://3v4l.org/AIi0B) at it, may help you on your way – jibsteroos Apr 30 '21 at 08:19
  • https://3v4l.org/DOjOn ? – mickmackusa Apr 30 '21 at 09:04
  • @And I added an answer to the duplicate page. I believe you will want the second snippet in my answer. – mickmackusa May 01 '21 at 07:30

0 Answers0