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

[Help Needed] in_array() expects parameter 2 to be array

Discussion in 'Facepalm' started by InspectorGadget, Feb 18, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    HI! I've been working on a fix on this. I need some assistance on this. Any help would be appreciated..

    Error:
    PHP:
    in_array() expects parameter 2 to be array, null given in /plugins/ChatterBot/src/RTG/ChatterBot/Main.php on line 95
    Code:
    PHP:
        public function onChat(PlayerChatEvent $e) {
          
            
    $msg strtolower($e->getMessage());
            
    $p $e->getPlayer();
            
    $n $p->getName();
            
    $in strtolower($this->cfg->get("interactions"));
              
                if(
    in_array($msg$in)) {
                  
                    switch(
    mt_rand(13)) {
                      
                        case 
    "1":
                            
    $p->sendMessage("Hey, " $n "!");
                        break;
                        case 
    "2":
                            
    $p->sendMessage("Holla, " $n "!");
                        break;
                        case 
    "3":
                            
    $p->sendMessage("Bonjour, " $n "!");
                        break;
                  
                    }
                      
                }
                else {
                    
    $this->getLogger()->warning("Error, not found!"); // DEBUG line for testing
                
    }
              
        }
    Config:
    Code:
    ---
    # Author: IG
    # For better usage and reply from the vote, please use the word 'bot' infront of your question or message! ( not mandatory ).
    # Don't worry about 'upper case' or 'lower case', this bot will read it :D
    interactions:
        - "Heya"
        - "hey"
    voting:
        - "Your voting question"
        - "How to vote?"
        - "Add how many as you want!"
    votingsite:
        - "Your voting site's link"
        - "You can have multiple links!"
    ...
    
    Full Code: https://github.com/RTGDaCoder/ChatterBot
     
  2. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    Try without using space on the config like
    Code:
    interactions:
    - "Hello
    - "hey"
    
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Does that make difference?
     
  4. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    I think so! try it.
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Error:
    Code:
    18.02 02:32:03 [Server] INFO Warning: in_array() expects parameter 2 to be array, null given in /plugins/ChatterBot/src/RTG/ChatterBot/Main.php on line 95 
    18.02 02:32:03 [Server] INFO Warning: strtolower() expects parameter 1 to be string, array given in /plugins/ChatterBot/src/RTG/ChatterBot/Main.php on line 93
    
     
  6. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    Ohh! Got it! Remove the strtolower() function!
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I'd need the plugin to get all the messages lower case instead of upper case. It won't be doing that if i remove
    PHP:
    strtolower()
     
  8. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    remove the strtolower() of $in cause the strtolower() needs string and you given array!
     
  9. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Then foreach the array of strings, strtolower them then cache/store the result.
     
    HimbeersaftLP and corytortoise like this.
  10. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    y=f(x)
    [y] != f([x]);
    [y] = array_map("f", [x]);
    because f() only converts a simple x, not an array of x
    Use array_map() to call f() per entry of x.

    PHP:
    $in array_map("strtolower"$interactions);
    WTF. Could you please kindly shut up if you can't make any sense of what you're saying?
     
  12. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Thanks SOFe, appreciate it!
     
  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.