Laravel 5.1 在 .env 文件中创建数组变量以供全局使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32046763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:12:23  来源:igfitidea点击:

Laravel 5.1 create array variable in .env file for global use

phplaravellaravel-5.1

提问by Keval Garala

Actually, I want an arry like this in .env file, I do not have any idea to create an array variable in .env file.

实际上,我想要在 .env 文件中像这样的 arry,我不知道在 .env 文件中创建数组变量。

 VARIABLE_NAME = [
    [0]  => 'Value 1',
    [1] => 'Value 2',
    [3] => 'Value 3',
    .................

];

回答by jedrzej.kurylo

You can' store an array in .inifile as the INIformat doesn't support that.

您不能在.ini文件中存储数组,因为INI格式不支持。

A workaround could be serializing the array to a string of some know format, e.g. comma separated values, and then split it whenever you needed.

解决方法可能是将数组序列化为某种已知格式的字符串,例如逗号分隔值,然后在需要时将其拆分。

This should do the trick:

这应该可以解决问题:

#.env file
VARIABLE_NAME="Value 1,Value 2, Value 3"

#config/app.php
return [
  'VARIABLE_NAME' => explode(',', env('VARIABLE_NAME'))
];