使用PHP从数组中查找内部的低值和高值

s2j5cfk0  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(182)

我想知道什么是最好的方法来解决这个问题。我试图使一个插值公式从Excel到PHP。Excel公式几乎是这样的:
(价值400 k-价值350 k)/((400 k-350 k)/1000)*((360 k覆盖范围-价值350 k)/1000)所有这些加上(价值350 k)
现在,我在数组中设置了coverage,如下所示:

$keyFactorArray = array(
    "75000" => "0.923",
    "80000" => "0.938",
    "85000" => "0.954",
    "90000" => "0.969",
    "95000" => "0.984",
    "100000" => "1",
    "110000" => "1.03",
    "120000" => "1.061",
    "130000" => "1.092",
    "140000" => "1.122",
    "150000" => "1.153",
    "160000" => "1.208",
    "170000" => "1.261",
    "180000" => "1.315",
    "190000" => "1.369",
    "200000" => "1.422",
    "250000" => "1.692",
    "300000" => "1.961",
    "350000" => "2.23",
    "400000" => "2.499",
    "450000" => "2.769",
    "500000" => "3.038",
    "550000" => "3.307",
    "600000" => "3.577",
    "650000" => "3.846",
    "700000" => "4.115",
    "750000" => "4.385",
    "800000" => "4.654",
    "850000" => "4.923",
    "900000" => "5.193",
    "950000" => "5.462",
    "1000000" => "5.731"
  );

因此,我的覆盖率可以是任何值,如350 k或240 k等。我如何找到高于或低于的索引值。我不太确定我是否以最有效的方式处理这个问题,只是继续我当前的知识,以及过去用其他代码“显示”的内容。简而言之,计算结果如下:
((2.499 - 2.23)/((2.499-2.23)/1000)*((360000 - 350000)/1000))全部+ 2.23
我希望有人能给我指出正确的方向,如果使用数组将是最有效的方式来做这个计算,或者我应该只是把它放在一个表,并使用SQL代替可能(不是最好的...)?

yeotifhr

yeotifhr1#

感谢@Alex Howansky并为我指出了正确的方向!以下是我的代码,帮助我解决了数组的数学问题:

foreach($keyFactorArray as $key => $value) {
  if($covA < $key) {
    $lowLimitKey = $prevKey;
    $lowLimitValue = $prevValueFactor;
    $highLimitKey = $key;
    $highLimitValue = $value;
    break;
  }
  $prevKey = $key;
  $prevValueFactor = $value;
};
$aopKeyFactor = ((($highLimitValue - $lowLimitValue) / (($highLimitKey - $lowLimitKey)/1000) * (($covA - $lowLimitKey)/1000))) + $lowLimitValue;

我肯定有更好的方法,但这是最有意义的方式,我和它的工作!

相关问题