後,我有這樣的方法: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'。 –