diff --git a/README.md b/README.md index e9d4423..cc0fed6 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,14 @@ Use the `setKeys($data)` method. Example: ] ]); +Alternatively, you can also provide an associative array of keys and values: + + $file = DotenvEditor::setKeys([ + 'ENV_KEY_1' => 'your-value-1', + 'ENV_KEY_2' => 'your-value-2', + 'ENV_KEY_3' => 'your-value-3', + ]); + ###### Delete a setter line in buffer Use the `deleteKey($key)` method. Example: diff --git a/src/Jackiedo/DotenvEditor/DotenvEditor.php b/src/Jackiedo/DotenvEditor/DotenvEditor.php index 9f05078..f1dc6c6 100644 --- a/src/Jackiedo/DotenvEditor/DotenvEditor.php +++ b/src/Jackiedo/DotenvEditor/DotenvEditor.php @@ -327,7 +327,18 @@ public function addComment($comment) */ public function setKeys($data) { - foreach ($data as $setter) { + foreach ($data as $i => $setter) { + if (!is_array($setter)) { + if (!is_string($i)) { + continue; + } + + $setter = [ + 'key' => $i, + 'value' => $setter, + ]; + } + if (array_key_exists('key', $setter)) { $key = $this->formatter->formatKey($setter['key']); $value = array_key_exists('value', $setter) ? $setter['value'] : null;