后,我有这样的方法:PHP - 非法偏移类型,is_array和is_object
public function setVariable($variable, $value = null)
{
$variables = json_decode($this->variables);
if(is_array($variable) || is_object($variable))
foreach($variable as $key => $value)
if(in_array($key, $this->variableNames))
$variables[$key] = $value;
else
$variables[$variable] = $value;
$this->variables = json_encode($variables);
$this->save();
}
但是,如果我这样调用方法:
setVariable(['test' => 'test', 'bla' => 'bla'])
它返回此错误:
ErrorException in User.php line 60:
Illegal offset type
第60行是这一行:
$variables[$variable] = $value;
但是,它为什么会返回错误? 我检查$ variable是数组还是对象, 但是它会继续返回这个错误。为什么?
因为你和php不同地考虑'else'。 –