0

I want to know how to go about solving this equation: I have a set of numbers - for example :

130 136 142 148 149 159

( I have highlighted the value of interest 148).

The difference between the values are :

6 6 6 1 10 ( I have highlighted the value of interest 1 ).

The reason that 148 is the first value of interest is because, value to value, it has the smallest number (1).

The Scenario

The set of numbers are inputs which are submitted to a database. Then I've started the calculation like this :

    $st1 = $userRow['val2(130)'] - $userRow['val1(136)'];
    $st2 = $userRow['val3(142)'] - $userRow['val2(130)'];
    $st3 = $userRow['val4(148)'] - $userRow['val3(142)'];
    $st4 = $userRow['val5(149)'] - $userRow['val4(148)'];
    $st5 = $userRow['val6(159)'] - $userRow['val5(149)'];

Which gives me the differences - value to value, and below gives me the min value.

    $val = min($st1, $st2, $st3, $st4, $st5);

So after that how do I isolate 148 or $userRow['val4(148)'] as the value of interest? Bearing in mind that a set of numbers could be any combination, but that the complete range will always be increasing, for example:

130 140 149 159 167 169

110 120 130 132 150 160

P.S I know the syntax is messy - but it's just for explanation purposes. Thanks

2
  • Using mind finds the difference but it tells you nothing about which values gave you that difference. You need to pull ID's out of the database as well as values and then write your own "min" checker Commented Mar 22, 2017 at 12:36
  • Ok - I'll check the PHP manual - thanks @ CaldwellYSR Commented Mar 22, 2017 at 12:41

2 Answers 2

2

I don’t know what this weird syntax: $userRow['val2(130)'] is supposed to be; if $userRow was the result of a database query fetch, then it would mean you have columns named val2(130), etc., which makes little sense.

And even if you just meant $userRow['val2'] here, it would still indicate that you should probably refactor your table layout - numbered column names are almost always an indication that you are doing things wrong. Most likely this should rather be properly normalized.


Anyway, get the values into a structure that you can easily loop over - such as an array, then this is pretty trivial:

$values = array(130, 136, 142, 148, 149, 159);

$minDiff = PHP_INT_MAX; // initialize with highest possible value
$element = null;

for($i=1, $l=count($values); $i<$l; ++$i) { // loop over all items from 2nd to the last one
  $diff = $values[$i] - $values[$i-1]; // calculate difference between current
                                       // and previous item
  if($diff < $minDiff) { // is the difference lower than the current minimal difference?
    $element = $values[$i-1]; // previous element becomes our current "result"
    $minDiff = $diff; // current difference becomes the new minimum
  }
}
var_dump($element);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you - it works. But when I input a different set of values the "result" stays the same? @CBroe
Apologies - it's sorted - Thanks for your help. @CBroe
0

It's really simple if you can put your values into an array or any other sort of list which gives you indeces to work with.

<?php
$mySet = array(130, 136, 142, 148, 149, 159); //your numbers

$mySet_val = array();
for($i = 0; $i < (count($mySet) - 1); $i++){
  $mySet_val[] = $mySet[$i] - $mySet[$i+1]; //calculate the differences
}
$key = array_keys($mySet_val , min($mySet_val)); //find the index of the smallest value
echo $mySet[$key]; //your corresponding value

I'm not sure what kind of array your $userRow is, but see if $mySet = $userRow works.

Comments

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.