1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

A better way to do this?

Discussion in 'Off-Topic' started by Muqsit, Apr 17, 2017.

  1. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I have 2 arrays ($scores and $players).

    $scores description:
    key -> playerId
    value -> score

    $players
    key -> playerId
    value -> playerName

    Is there a better way than this to replace keys of $scores with values from $players?

    PHP:
    <?php

    //playerId => score
    $scores = [10 => 3499412 => 2384813 => 48939];

    //playerId => playerName
    $players = [10 => 'Muqsit'12 => 'NotMuqsit'13 => 'entity303'];

    $res = [];
    foreach (
    $players as $k => $v) {
        
    $res[$v] = $scores[$k];
    }

    //$res = ['Muqsit' => 34994, 'NotMuqsit' => 23848, 'entity303' => 48939];
     
  2. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Why not use a two dimensional array with playerID as the key, and an array of name and score as the value?

    Edit: since your unique ID could just be the players name, do you even need playerID? If so, I'd use the player name (or a hash) as the key anyway... but it depends what you are trying to achieve.
     
    Last edited: Apr 17, 2017
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    offtopic using custom objects?
    other then that clueless...
     
  4. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Your code timing (microtime): 5.0067901611328
    My code timing (microtime): 2.3841857910156

    PHP:
    <?php

    //playerId => score
    $scores = [10 => 3499412 => 2384813 => 48939];

    //playerId => playerName
    $players = [10 => 'Muqsit'12 => 'NotMuqsit'13 => 'entity303'];

    foreach (
    $scores as $k => &$v){
        
    $v $players[$k];
    }
    This is much faster because you're passing by reference rather than value, basically meaning it's real value instead of a copy (I guess you know that already).
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Passing by reference doesn't make the code execute faster, but that's a nice way.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It isn't the answer still, not looking for alternatives.
     
  7. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    It does save you memory because it doesn't have to waste bytes to create a copy of the array.
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Not necessarily fast though. It can be easily proven as a myth.
     
  9. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    This is another method you could use:

    PHP:
    <?php

    //playerId => score
    $scores = [10 => 3499412 => 2384813 => 48939];

    //playerId => playerName
    $players = [10 => 'Muqsit'12 => 'NotMuqsit'13 => 'entity303'];

    $res array_combine(array_keys($players), array_values($scores));
    Can't get any faster than this, m8.
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Wow, okay. That's what I was looking for. Thanks <3
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.