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

Solved Array

Discussion in 'Development' started by xXNiceAssassinlo YT, Aug 19, 2018.

  1. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    ok here how I set it
    PHP:
    private $test = [];

    $this->test[] = " " $player->getName();
    How would I echo “1" only (I need name for something else)
     
  2. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Why save as a string and not an array, or use the player name as the key?
     
    Muqsit and corytortoise like this.
  3. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What context is this? If each name will have a different number, use the number as the key for the string entry. If each player will have a number, possibly duplicate, do what DaPigGuy mentioned. If you need both with a different key, save it as a multidimensional array or string with a semicolon or something that you can use to explode later on.
     
    Primus likes this.
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    PHP:
    $this->test[] = "1Steve";

    $number intval($this->test[0]{0}); // int 1
    $name substr($this->test[0], 1); // Steve
    Just answer the damn question
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's hacky. It will fail when the number is more than two digits long. And you forgot to ltrim the name (also, he needs help with "1 Steve" not "1steve").
    It's either a solution or a quality solution.
     
    corytortoise likes this.
  6. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Maybe saving as number:name
    Like $this->test[] = "94:Steve"
    He can $value = explode(":", "94:Steve"); to get the values. So $value[0] is the number and $value[1] the name
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Or you could save yourself from exploding the string and casting the number to int (or ignore the casting) everytime, if you used the number as the index if that's a possibility.
     
    MalakasPlayzMCPE likes this.
  8. Primus

    Primus Zombie Pigman

    Messages:
    749
    I'm obviously taking a piss. Perhaps store the number digits count along with this hash? (Again - taking a piss)
     
    Last edited: Aug 19, 2018
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    Why people ignore such good posts as Yours?

    I would go with one of these two solutions
    PHP:
    $this->test[1] = "Steve";
    $this->test["Steve"] = 1;
    Unless you're writing to file you don't have to do any encoding/decoding (wtf anyway)
     
    xXNiceAssassinlo YT and Muqsit like this.
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i would go with
    $test[] = [name,num]
    if the above isnt possible (say number not unique)
     
  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.