phparraysfor-loop

How to identify if given number is there in a given array range?


I have an array which stores numbers. The count of numbers varies time to time. There is another variable which a holds number, and I need to find out in which range $num falls. In the above case $num falls under 64. (Bigger than 32 and less than 64)

$ar = array(0, 32, 64, 96, 128, 160, 192, 224);

$num = 44;

How do I crack this?


Solution

  • $ar = array(0, 32, 64, 96, 128, 160, 192, 224);
    
    $num = 44;
    
    $range = min(array_filter($ar, function($i) use($num) {
        return $i > $num;
    }));
    
    var_dump($range);
    

    Online demo: http://ideone.com/KV6MWD