Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

foreach doesn't work!

Comments in 'Plugin Development' started by Kvetinac97, Sep 26, 2015.

  1. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Hi.

    In my plugin, I used following configurations:

    questions.yml
    Code:
    "mycontent":
     - "CORRECT_something"
     - "something else"
    
    Main.php:
    PHP:
    //on startup:
    $this->questions = new Config($this->getDataFolder()."questions.yml"Config::YAML);
    //some code
    $myarray $this->questions->getAll();
    foreach (
    $myarray as $question => $answers){ //this foreach worked
    $this->curr_aw = [$question => $answers];
    //some code
     
    foreach($this->curr_aw as $question => $answers){
     foreach (
    $answers as $answer){ //this foreach doesn't work as I expect
    $this->getLogger()->info($answer); //when this is called, I get only "something", but I don't see any messages like "something_else")
     
    if (stripos($answer,"CORRECT_" !== false)){
    Why this foreach doesn't work? What should I change? Or it's bug in new PocketMine version?
    PianoPlayer38 and TheKofola12 like this.
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Does it not work at all, or does it do something unexpected?
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    Foreach is not pocketmine function xD
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    He knows that, but I don't think you know. Read it closely.
  5. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
  6. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    It's foreach, so it should perform action for ¬Ľall elements of the array, but it performs it only for first. For each answer, I check if it's correct, but it check only first answer written in questions.yml.
  7. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Please don't post if you don't have anything to say. Stop spamming. All your posts have words but no useful content. Please stop trying to increase your post count by spamming.
    Taha_The_Hacker likes this.
  8. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc

    Ummm
    Praxthisnovcht likes this.
  9. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Try using :
    Code:
    "mycontent":
    - 'CORRECT_something'
    - 'something else'
    Because else is an PHP function.
  10. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Doesn't work. I tried also change values to other strings, sentences, but still doesn't work :/
    PianoPlayer38 and TheKofola12 like this.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    @Gamecrafter, you know, I'm seriously annoyed of you.
    It is a control structure, not a function.
    Try doing var_dump($this->curr_aw) in the last part then you'll know something is wrong. Make sure that you understand how key=>value works in PHP.
  12. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    That's what makes me Gamecrafter. I'm unique. :p
    TheKofola12 and Minecrafter9203 like this.

Share This Page

Advertisement