php - figure out proper decremental values between a range -
i'm not quite sure how phrase question succinctly... also, i'm sure boils down basic math failure. anyways, goal count 1 20, assign decremented value count, starting 80 , ending in 2.
so basically,
$array = ( 1 => 80, .. 20 => 2 );
here code have come with, , seems close it's off, , can't figure out why :(
$array = array(); for( $x = 1; $x <= 20; $x++ ) { $y = ( 80 - 2 ) / 20; $p = ( 80 - ( $x * $y ) ) + ( $y / $x ); $array[$x] = $p; } echo "<pre>"; print_r($array); echo "</pre>";
this gives me:
array ( [1] => 80 [2] => 74.15 [3] => 69.6 [4] => 65.375 [5] => 61.28 [6] => 57.25 [7] => 53.2571428571 [8] => 49.2875 [9] => 45.3333333333 [10] => 41.39 [11] => 37.4545454545 [12] => 33.525 [13] => 29.6 [14] => 25.6785714286 [15] => 21.76 [16] => 17.84375 [17] => 13.9294117647 [18] => 10.0166666667 [19] => 6.10526315789 [20] => 2.195 )
could point out i'm going wrong or nudge me in right direction?
what want is:
$array = array(); ($x=1;$x<=20;$x++) { $y = (80-2)/(20-1); $p = 80-($x-1)*$y; $array[$x] = $p; }
explanation: start 80
, end 2
. indexes go 1
20
. means, have 20 elements, first , last fixed. so, have 20-1 = 19 "jumps" (or subtractions) until reach 2 80. each jump has same size. since total difference 80-2, each subtraction should have (80-2)/(20-1) = 78/19 size.
that's why should write
$p = 80-($x-1)*$y;
note ($x-1)
due fact subtraction starts on second element (you want start 80).
Comments
Post a Comment