-3

i am new to php , i am running in to problem, i have to pass an array as an argument in php function , when i am doing this i am getting array to string error .could you guys help me??

This is the helper function:

  private function userinfo($info,$args=''){
            try{
                if($args===''){
                    return $this->user->authenticate($this->username,$this->pswd,$info);
                }
                return $this->user->authenticate($this->username,$this->pswd,$info,$args);
            }catch(exception $e){


            }
        }

this is the add function called :

   public function addUser($id,$email,$name){
        $this->userinfo('add',array(0=>$id,1=>$email,2=>$name));
    }
lakshmi
  • 1
  • 1
  • Possible duplicate of [How to solve PHP error 'Notice: Array to string conversion in...'](https://stackoverflow.com/questions/20017409/how-to-solve-php-error-notice-array-to-string-conversion-in) – Qirel Jul 19 '17 at 18:22

2 Answers2

0

Try this :

 private function userinfo($info,$args = array()){
            try{
                if(empty($args)){
                    return $this->user->authenticate($this->username,$this->pswd,$info);
                }
                return $this->user->authenticate($this->username,$this->pswd,$info,$args);
            }catch(exception $e){


            }
        }



   public function addUser($id,$email,$name){
        $this->userinfo('add',array(0=>$id,1=>$email,2=>$name));
    }
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
0

you are defining a string in function parameter but passing an array to function please modify your code like:

 private function userinfo($info, $args = []) {
   try {
     if (!is_array($args) {
       return $this->user->authenticate($this->username, $this->pswd, $info);
     }
     return $this->user->authenticate($this->username, $this->pswd,$info, $args);
   } catch(exception $e) {

   }
}
Muhammad Usman
  • 1,403
  • 13
  • 24
  • 1
    You can still pass whatever you want, that's just the *default* value. If you require a specific type of variable, it'd look like `function userinfo($info, array $args = []) {`. The error comes from how that variable is being used in the code. – Qirel Jul 19 '17 at 18:21
  • i tried this way , but i am still getting array to string error – lakshmi Jul 19 '17 at 18:33