0

Coding in PHP, I currently have an array of 100 values, which looks like this:

$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?

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

where y is the incremental step in the scale.

Thank you for any help!

0

3 Answers 3

2

An alternative to using loops and all that would be range

$start=630;
$stop=600;
$steps=100;
$cmassarray=range( $start, $stop, ( ( $start-$stop ) / $steps ) );
Sign up to request clarification or add additional context in comments.

Comments

1

You might want to have a look at the range function, which takes an optional third argument for step. 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 );

Comments

0

Start with this code:

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

1 Comment

Downvoting for the use of a for-loop instead of the use of php's built in range function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.