1

I have a array with the lvl=>xp correspondance and I would make a function that return the lvl for a specific xp. like $lvl = getLvlOf(15084); //return 5

$lvl_correspondance = array(
    1=>100,
    2=>520,
    3=>2650,
    4=>6588,
    5=>12061,
    6=>23542,
    ...
    n=>xxxxxx
);

I search the easyest and ressourceless way to do it.

Sorry for my poor english :(

0

5 Answers 5

4

Assuming the level values in the array are kept sorted, e.g. (it's 100,200,300, 400, etc... and not 200,500,100,300,400), then a simple scan will do the trick:

$xp = 15084;
$last_key = null;
foreach($lvl_correspondenance as $key => $val) {
    if ($val < $xp) {
        $last_key = $key;
    } else {
        break;
    }
}

That'll iterate through the array, and jump out as soon as the XP level in the array is larger than the XP level you're looking for, leaving the key of that "last" level in $last_key

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

2 Comments

Shouldn't it be if ($val <= $xp) ?
Depends on if you get bumped to a higher level on reaching 100 (say) or at 101, I guess.
1
function getLvlOf($lvl, $int){
        foreach($lvl as $level=>$exp){
                if($exp > $int){
                        return $level-1;
                }
        }
}

it's O(n) so no magic there...

Comments

1

It looks like your array can be computed live -

XP = exp( 3 * ln(LVL) + 4 ) * 2

You can do the same in reverse, in O(1):

LVL = exp(ln(XP/2) - 4 / 3)

I rounded the equation, so there may be a +/- 1 issue

Good Luck!

3 Comments

It a perfect hasard if it can be computed live. I already try to do this but there is 100 lvl and I didn't find formul for that. But thanks a lot for helping.
May be you can find a formul with the real data here spreadsheets.google.com/… ?
Best fit is a 4-order polynomial: XP = 0.014*L^4 - 1.83*L^3 + 108.67*L^2 - 1563.36*L + 6830 this is ridiculous, and it would be better if you knew where the numbers came from originally
0

Not good if you have very high level values, but:

$lvl = $lvl_correspondance[array_search(
                                 max(array_intersect(
                                           array_values($lvl_correspondance),
                                                        range(0,$points)
                                                       )
                                    ),
                                 $lvl_correspondance
                                )];

2 Comments

Its a great idea but its realy realy bad performance for my type of value. Thanks for helping.
Never said it was efficient, just an alternative way of doing it :-)
-2

You can use array_flip if the xp levels are distinct. Then you can simply access the level number using the xp as index:

$levels = array_flip($lvl_correspondance);
$lvl = $levels[15084];

EDIT: But maybe a function would be better to fetch even the xp levels in between:

function getLvlOf($xp) {
  // get the levels in descending order
  $levels = array_reverse($GLOBALS['lvl_correspondance'], true);

  foreach ($levels as $key => $value) {
    if ($xp >= $value)
      return $key;
  }

  // no level reached yet
  return 0;
}

4 Comments

And if the level doesn't exist, you get null. OP wants "nearest" value.
It's not a look up, it's a value between two levels
Just make sure that values are all unique. From the php manual If a value has several occurrences, the latest key will be used as its values, and all others will be lost.
@Marc B: the OP said "return the lvl for a specific xp". I gave a solution to that. Right after that I've editted in a solution that I find better.

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.