Wednesday, August 10, 2016

[php][Resolved] Illegal offset type

When you get this  "Illegal offset type" error may means you used an array or object as the index of an array, or key of object. For an example, this case used an array as object key cause the error:
    public function getUserData()
    {
        $temp = ['id','user_name', 'email'];
        $user  = [];
        foreach($temp as $index){
            $user->$temp = $this->attributes[$temp];
        }
        return $user;
    }   

Corrected :
    public function getUserData()
    {
        $temp = ['id','user_name', 'email'];
        $user  = [];
        foreach($temp as $index){
            $user->$index = $this->attributes[$index];
        }
        return $user;
    }   

Reference:
http://stackoverflow.com/questions/2732451/php-how-do-i-fix-this-illegal-offset-type-error

No comments :

Post a Comment