如何使用多维数组中的值对多维数组进行排序?

时间:2021-07-10 10:05:17

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

我正在开发一个测试,它将得到2个值的结果

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

所以我的问题是获得结果徽章的最有效方法是什么?也许有更好的方法吗?

1 个解决方案

#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

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

#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

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