更新:下面是完整的控制器方法:PHP數組不會assing鍵值
protected $game = [];
private function stageAction($type, $actions)
{
if (env('APP_DEBUG')) {
Log::info('STAGE ACTION started with $type= '.$type.' *********************');
}
$message = [];
$this->game['sys_msg'] = [];
$this->game['stage_action'] = [];
switch ($type) {
case 'attack_end':
$message['action'] = 'ignore';
$message['message'] = '<b>'.active_player()['nickname'].'</b> ends their attack.';
$this->game['sys_msg'] = $message;
break;
case ......
break;
default:
}
// If it is an "ignored" message then send it now
if ($this->game['sys_msg']['action'] == 'ignore') {
$this->broadcastSysMsg();
}
if (env('APP_DEBUG')) {
Log::info('SYS MSG - '.print_r($this->game['sys_msg'], true));
}
在我Laravel控制器我有這樣一個全局屬性:
protected $game = [];
在一個方法,我給它加上這樣的一個鍵:
$this->game['sys_msg'] = [];
然後,添加值,它是這樣的:
$message['action'] = 'ignore';
$message['message'] = 'My message is blah blah blah...';
$this->game['sys_msg'] = $message;
代碼再往下我通過登錄其輸出這樣的檢查是:
Log::info('SYS MSG - '.print_r($this->game['sys_msg'], true));
但我得到這個:
[]
我想分配一個對象數組到這個值。爲什麼這不起作用,但沒有失敗?