简体   繁体   English

创建仅知道最大值和最小值的数组

[英]Create an array only knowing max and min values

Coding in PHP, I currently have an array of 100 values, which looks like this: 在PHP中编码,我目前有一个100个值的数组,看起来像这样:

$cmassarray = array(630.00,629.70,629.40,629.10,628.80,628.50,628.20,627.90,627.60,627.30,627.00,
                626.70,626.40,626.10,625.80,625.50,625.20,624.90,624.60,624.30,624.00,623.70,
                623.40,623.10,622.80,622.50,622.20,621.90,621.60,621.30,621.00,620.70,620.40,
                620.10,619.80,619.50,619.20,618.90,618.60,618.30,618.00,617.70,617.40,617.10,
                616.80,616.50,616.20,615.90,615.60,615.30,615.00,614.70,614.40,614.10,613.80,
                613.50,613.20,612.90,612.60,612.30,612.00,611.70,611.40,611.10,610.80,610.50,
                610.20,609.90,609.60,609.30,609.00,608.70,608.40,608.10,607.80,607.50,607.20,
                606.90,606.60,606.30,606.00,605.70,605.40,605.10,604.80,604.50,604.20,603.90,
                603.60,603.30,603.00,602.70,602.40,602.10,601.80,601.50,601.20,600.90,600.60,
                600.30,600.00);

All of the steps are the same length, and I may need to change them (and/or the max/min values) at a future stage, so I would like to find a way to avoid having to re-calculate them manually and type them each time. 所有步骤的长度都是相同的,我可能会在以后的阶段中更改它们(和/或最大/最小值),因此我想找到一种方法来避免必须手动重新计算并键入他们每次。

If I know the maximum value is 630.00 and the minimum value is 600.00, and that I have 100 steps, would it be possible to create an array specifying that each value is an increment on this equation? 如果我知道最大值是630.00,最小值是600.00,并且我有100步,是否可以创建一个数组,指定每个值都是该方程式的增量?

x (array value) = 600+((Max-Min)/100)*y) 

where y is the incremental step in the scale. 其中y是刻度的增量步长。

Thank you for any help! 感谢您的任何帮助!

An alternative to using loops and all that would be range 使用循环和所有这将是另一种range

$start=630;
$stop=600;
$steps=100;
$cmassarray=range( $start, $stop, ( ( $start-$stop ) / $steps ) );

You might want to have a look at the range function, which takes an optional third argument for step. 您可能想看看range函数,该函数需要一个可选的第三个参数作为步骤。 This argument you can easily derive from your maximum, minimum and amount of steps. 您可以轻松地从最大,最小和步数中得出该参数。 Here's an example: 这是一个例子:

$max = 630;
$min = 600;
$steps = 100;
$step = ($max - $min) / $steps;
$your_result = range( $max, $min, $step );

Start with this code: 从以下代码开始:

$max = 630;
$min = 600;
$steps = 100;
$step = ($max - $min) / $steps;
$ar = [];
for ($i = $max; $i >= $min; $i -= $step) {
    $ar[] = $i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM