Php 数组索引没有按应有的方式运行?

Php 数组索引没有按应有的方式运行?,php,laravel,Php,Laravel,我有下面的代码,它让我发疯 echo $this->year; //This echoes 2019 return $set_year[2019]; //This works, it returns the expected value return $set_year[$this->year]; //This returns nothing and gives me an 'Undefined Index' 这是怎么回事?下面是我如何设置数组的: return [ 'ye

我有下面的代码,它让我发疯

echo $this->year; //This echoes 2019
return $set_year[2019]; //This works, it returns the expected value
return $set_year[$this->year]; //This returns nothing and gives me an 'Undefined Index'
这是怎么回事?下面是我如何设置数组的:

return [
    'years' => [

        2019 => 7,
        2018 => 8,
        2017 => 9,
        2016 => 10,
        2015 => 11,
        2014 => 12,
        2013 => 13,

    ],

];

这起作用了。。。但我还不知道为什么

$set_year = Config::get('sitevars.years');
return $this->year ? $set_year[$this->year] : 0;

var\u转储什么($this->year)给你…?var_转储什么($this->year);模具();显示?我引用:“如果从函数中调用,return语句将立即结束当前函数的执行,并将其参数作为函数调用的值返回。return还将结束eval()语句或脚本文件的执行。”-所以第二次返回永远不会发生。@PatrickJanser似乎对我有效,不管int或string@PatrickJanser在PHP中应该不会有什么不同,除非$this->year既不是字符串也不是整数,可能是日期格式?