How to simplify this code

$amount = 100; / / 110 br 90200
$arr = [98100100120150160183];

as above: the variable $amount is compared with $arr. If the variable is in the array, it returns the subscript value in the array;
if the variable is not in the array, first detect whether the variable is between which two values (AB), and if so, return the subscript of the AB;
if it is not between the two values, if the variable is less than the first value of the array, return subscript 0;
if the variable is greater than the last value of the array, return the array length by one.

:
$amount = 100;     [1,2];
$amount = 110;     [2,3];
$amount = 90;      [0];
$amount = 200;     [6];

the following is the code I wrote to simplify.

$key   = [];    //
$count = count($arr) - 1;

foreach($arr as $k => $v){
    if($amount == $v){
        $key[] = $k;
    }
}
if(empty($key)){
    foreach($arr as $k => $v){
        if($amount < $arr[0]){
            $key[] = "";
            break;
        }
    }
}
if(empty($key)){
    foreach($arr as $k => $v){
        if($amount > $arr[$count]){
            $key[] = $count;
            break;
        }
    }
}
if(empty($key)){
    foreach($arr as $k => $v){
        if($amount >= $arr[$k] && $amount < $arr[$k + 1]){
            $key[] = $k;
            $key[] = $k + 1;
        }
    }
}
var_dump($key);
Php
Apr.28,2021

just saw this question, I thought of the binary search method, but there is a problem is that the array elements can be repeated, but also return the index, of all the duplicate values, so when we find a match, we have to do a while

.
<?php
$arr = [100,100,100,100,120,150,160,183,200];

function find($x,$arr){
  $low = 0;
  $len = count($arr);
  $high =$len-1;
  $result = [];
  

  
  while ($low <= $high){
    $middle = (int)round(($high + $low ) / 2);

    if($x == $arr[$middle]){
      //result = [middle]
      while($low<=$high){
        if($arr[$low] === $x){
            array_push($result,$low);
        }
        
        $lowPP;
        
      }
      
      break;
    } else if ($x>$arr[$middle]){
      $low = $middle+1;
    } else {
      $high = $middle-1;
    }
  }

  
  if(count($result) === 0 ){
    $result =  [$high,$low];

    if($result[0]<=0){
        return [0];
      }
      
      if($result[0] === $len-1 ){
        return [$len-1];
      }
  }
    
  return $result;
}

$d = find(100,$arr);

var_dump($d);

I wrote it in javascript to avoid using the built-in functions of the language as much as possible. In addition, both php and javascript belong to the language of c like, so this code from javascript to php basically adds a $
javascript in front of the variable. You can see the following
http://jsbin.com/lazuwexuji/1.

.

my version should be the simplest

function calcIndex($value, $resource = [])
{
    if ($value < min($resource)) {
        return [0];
    } else if ($value > max($resource)) {
        return [count($resource) - 1];
    }
    $result = [];
    foreach ($resource as $index => $item) {
        if ($item == $value) {
            $result[] = $index;
        } else if ($index < count($resource) - 1 && $value > $item && $value < $resource[$index + 1]) {
            $result = [$index, $index + 1];
        }
    }
    return $result;
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4dc25-44f71.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4dc25-44f71.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?