0

我正在开发一个测试,它会给出 2 个值的结果

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

并且我有范围将作为徽章名称从数组到该结果给出响应:

$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)
    ),
); 

徽章数组对应于一个看起来像这样的网格

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

所以我的问题是为我的结果获得徽章的最有效方法是什么?也许有更好的方法来解决这个问题?

4

1 回答 1

2

在我的脑海中,你可以做这样的事情。

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;
      }
   }
}

此代码未经测试。但应该工作

于 2012-09-11T08:23:08.913 回答