Skip to content

Instantly share code, notes, and snippets.

@wesamly
Last active July 25, 2023 10:17
Show Gist options
  • Save wesamly/20852a46451e611bb99a09de8af95650 to your computer and use it in GitHub Desktop.
Save wesamly/20852a46451e611bb99a09de8af95650 to your computer and use it in GitHub Desktop.
Laravel Array Environment Variable
MY_KEY=key1|value1,key2|value2
<?php
//Save as config/sample.php
return [
'key' => collect(explode(',', env('MY_KEY', '')))->pipe(function($collection) {
$arr = [];
foreach ($collection as $item) {
if (strpos($item, '|') === false) {
continue;
}
list($k, $v) = explode('|', $item);
$arr[$k] = $v;
}
return collect($arr);
})->toArray()
];
<?php
$data = config('sample.key');
/*
* Returns
* ["key1" => "value1", "key2" => "value2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment