这篇文章可能有意义,也可能没有意义,但我会尽力向我的能力解释。
因此,我们假设用户的EXP值为30
。
$level = array(
'1' => '0', '2' => '6', '3' => '13', '4' => '21', '5' => '29', '6' => '37', '7' => '46', '8' => '56', '9' => '66', '10' => '76', '11' => '87', '12' => '99', '13' => '111', '14' => '124', '15' => '138', '16' => '153', '17' => '168', '18' => '184', '19' => '201', '20' => '219', '21' => '238', '22' => '258', '23' => '279', '24' => '301', '25' => '325', '26' => '349', '27' => '376', '28' => '403', '29' => '432', '30' => '463', '31' => '495', '32' => '529', '33' => '565', '34' => '602', '35' => '642', '36' => '684', '37' => '729', '38' => '775', '39' => '824', '40' => '876', '41' => '931', '42' => '989', '43' => '1049', '44' => '1114', '45' => '1181', '46' => '1252', '47' => '1328', '48' => '1407', '49' => '1490', '50' => '1578', '51' => '1671', '52' => '1769', '53' => '1872', '54' => '1981', '55' => '2096', '56' => '2217', '57' => '2345', '58' => '2479', '59' => '2621', '60' => '2771', '61' => '2928', '62' => '3095', '63' => '3270', '64' => '3455', '65' => '3650', '66' => '3855', '67' => '4072', '68' => '4301', '69' => '4542', '70' => '4796', '71' => '5064', '72' => '5347', '73' => '5645', '74' => '5960', '75' => '6292', '76' => '6641', '77' => '7010', '78' => '7399', '79' => '7809', '80' => '8242', '81' => '8698', '82' => '9179', '83' => '9686', '84' => '10221', '85' => '10786', '86' => '11381', '87' => '12008', '88' => '12670', '89' => '13368', '90' => '14105', '91' => '14881', '92' => '15700', '93' => '16564', '94' => '17475', '95' => '18436', '96' => '19449', '97' => '20517', '98' => '21645', '99' => '22833'
);
现在,如果你在这个数组中匹配30。这意味着用户级别为5级,因为29级别为5级,而级别6则需要37级。
现在,我的问题是。我如何创建一个可以返回值为Level 5的函数。它可能是我在这里看到的closest
函数之一,但它必须返回key
的{{1}}数组就是等级所在的数组。
我真的不知道从哪里开始,我不知道这是怎么回事。我想象一下for语句,但我不知道我甚至在哪里开始。
再次,希望我解释得这么好......很难解释!
答案 0 :(得分:1)
function getClosest($search, $arr)
{
$closest = null;
$level = null;
foreach ($arr as $key => $item) {
if ($closest === null || abs($search - $closest) > abs($item - $search)) {
$closest = $item;
$level = $key;
}
}
return $level;
}
$level = array(
'1' => '0', '2' => '6', '3' => '13', '4' => '21', '5' => '29', '6' => '37', '7' => '46', '8' => '56', '9' => '66', '10' => '76', '11' => '87', '12' => '99', '13' => '111', '14' => '124', '15' => '138', '16' => '153', '17' => '168', '18' => '184', '19' => '201', '20' => '219', '21' => '238', '22' => '258', '23' => '279', '24' => '301', '25' => '325', '26' => '349', '27' => '376', '28' => '403', '29' => '432', '30' => '463', '31' => '495', '32' => '529', '33' => '565', '34' => '602', '35' => '642', '36' => '684', '37' => '729', '38' => '775', '39' => '824', '40' => '876', '41' => '931', '42' => '989', '43' => '1049', '44' => '1114', '45' => '1181', '46' => '1252', '47' => '1328', '48' => '1407', '49' => '1490', '50' => '1578', '51' => '1671', '52' => '1769', '53' => '1872', '54' => '1981', '55' => '2096', '56' => '2217', '57' => '2345', '58' => '2479', '59' => '2621', '60' => '2771', '61' => '2928', '62' => '3095', '63' => '3270', '64' => '3455', '65' => '3650', '66' => '3855', '67' => '4072', '68' => '4301', '69' => '4542', '70' => '4796', '71' => '5064', '72' => '5347', '73' => '5645', '74' => '5960', '75' => '6292', '76' => '6641', '77' => '7010', '78' => '7399', '79' => '7809', '80' => '8242', '81' => '8698', '82' => '9179', '83' => '9686', '84' => '10221', '85' => '10786', '86' => '11381', '87' => '12008', '88' => '12670', '89' => '13368', '90' => '14105', '91' => '14881', '92' => '15700', '93' => '16564', '94' => '17475', '95' => '18436', '96' => '19449', '97' => '20517', '98' => '21645', '99' => '22833',
);
echo getClosest(30, $level);
<强> WORKING DEMO 强>
答案 1 :(得分:0)
如果我理解这是正确的,您可以尝试以下(用c语言): (假设你匹配30)
int your_exp = 30;
int i, array_length = 6;
int EXP[] = {0, 6, 13, 21, 29, 37};
for(i=0; i<=array_length - 1; i++)
{
if(EXP[i] >= your_exp)
printf("Your level is: %d", i);
}
答案 2 :(得分:0)
你可以递归地这样做:
function getClosest($level, $input, $index)
{
$index++;
if($index == count($level))
return $index;
if($input < $level[$index+1] && $input >= $level[$index])
return $index;
return getClosest($level, $input, $index);
}
你用这种方式打电话:
echo getClosest($level, 13554, 0); //prints 89