8

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

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

];

1 Answer 1

21

You can't store an array in .env file as the ENV format doesn't support that.

A workaround could be serializing the array to a string of some known 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'))
];
Sign up to request clarification or add additional context in comments.

1 Comment

There is work around that allows also associative keys: laracasts.com/discuss/channels/general-discussion/… SETTING={"KEY1":"VALUE1","KEY2":"VALUE2"} $mydata = (array)json_decode(env('SETTING'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.