0

I am developing a test which will give a result of 2 values

$result = array( 't' => 10, 's' => 20 );

and i have ranges that will give a response as badge name from array to that result:

$badges = array(
    'badge1' => array(
        array('tLow' => 0, 'tHigh' => 20),
        array('sLow' => 0, 'sHigh' => 10)
    ),
    'badge2' => array(
        array('tLow' => 0, 'tHigh' => 10),
        array('sLow' => 11, 'sHigh' => 20)
    ),
    'badge3' => array(
        array('tLow' => 21, 'tHigh' => 30),
        array('sLow' => 0, 'sHigh' => 10)
    ),
    'badge4' => array(
        array('tLow' => 31, 'tHigh' => 40),
        array('sLow' => 0, 'sHigh' => 10)
    ),
    'badge5' => array(
        array('tLow' => 11, 'tHigh' => 30),
        array('sLow' => 11, 'sHigh' => 30)
    ),
    'badge6' => array(
        array('tLow' => 0, 'tHigh' => 10),
        array('sLow' => 21, 'sHigh' => 30)
    ),
    'badge7' => array(
        array('tLow' => 0, 'tHigh' => 10),
        array('sLow' => 31, 'sHigh' => 40)
    ),
); 

badge arrays correspond to a grid which looks like this

+---+
| 7 |
+---+---+---+
| 6 |       |
+---+   5   +
| 2 |       |
+---+---+---+---+
|   1   | 3 | 4 |
+---+---+---+---+

so my question is what is the most effective way to get a badge for my result? maybe there is a better approach to this?

3
  • 1
    Please add appropriate language tag. Commented Sep 11, 2012 at 8:00
  • 1
    i don't think it is language dependent in any way and i am fine with pseudo code. I dont want to filter out people who don't appreciate PHP :) Commented Sep 11, 2012 at 8:05
  • OK - there's a language-agnostic tag for that kind of question, but you should probably use pseudo code if the question is truly language agnostic, as not everyone can read php/perl/whatever. Commented Sep 11, 2012 at 8:34

1 Answer 1

2

Of the top of my head you could do something like this.

foreach ($badges as $key => $badge)
{
   if ($result['t'] >= $badge[0]['tLow'] && $result['t'] <= $badge[0]['tHigh'])
   {
      // t matches
      if ($result['s'] >= $badge[0]['sLow'] && $result['s'] <= $badge[0]['sHigh'])
      { 
         // s matches
         echo 'Badge was found: '.$key;
         break;
      }
   }
}

This code was not tested. But should work

Sign up to request clarification or add additional context in comments.

1 Comment

If you change $badge['tLow'] to $badge[0]['tLow'] and 3 others by same pattern in the if statments it will work fine. Also after echo its wise to put a break; statement so it stops after it finds something. Other than that thanks a lot!

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.