在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);
所有步骤都是相同的长度,我可能需要在未来阶段更改它们(和/或最大/最小值),所以我想找到一种方法来避免重新计算它们手动并每次键入它们。
如果我知道最大值是630.00且最小值是600.00,并且我有100步,是否可以创建一个数组,指定每个值是此等式的增量?
x (array value) = 600+((Max-Min)/100)*y)
其中y是比例中的增量步骤。
感谢您的帮助!
答案 0 :(得分:2)
使用循环的替代方法,以及range
$start=630;
$stop=600;
$steps=100;
$cmassarray=range( $start, $stop, ( ( $start-$stop ) / $steps ) );
答案 1 :(得分:1)
您可能希望查看range
函数,该函数采用可选的第三个参数作为步骤。您可以从最大,最小和步骤数量中轻松推导出此参数。这是一个例子:
$max = 630;
$min = 600;
$steps = 100;
$step = ($max - $min) / $steps;
$your_result = range( $max, $min, $step );
答案 2 :(得分:0)
从这段代码开始:
$max = 630;
$min = 600;
$steps = 100;
$step = ($max - $min) / $steps;
$ar = [];
for ($i = $max; $i >= $min; $i -= $step) {
$ar[] = $i;
}