2016-01-24 100 views
0

編碼在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是規模中的增量步驟。

謝謝你的幫助!

+3

使用'for'循環? –

回答

2

使用循環和所有的替代,這將是range

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

謝謝@RamRaider! –

0

開始使用此代碼:

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

謝謝!這似乎是在做的伎倆:) –

+0

Downvoting for for循環的使用,而不是使用內置'範圍'函數的PHP。 – Rein

1

你可能想看看range功能,它帶有一個可選第三步說明。您可以輕鬆地從您的最大,最小和步驟數量中推導出這個參數。這裏有一個例子:

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