0

How to output an each array element in a new line with Laravel Storage:put?

I have an array like below that I want to put into a file, array_test.txt, where each element goes on a new line.

$arrTest
array:2 [
  0 => "Val 1"
  1 => "Val 2"
]

Using Laravel's Storage, it goes like Storage:put($fileName, $arrTest); However the output is on a single line. Storage as an $options tag, but that seems to just be for setting files as public or private. If you had a link to what $options could be inputted, I'd like that, I can't find any documentation for what all the $options are for Storage:put!

Another option seems to be using Storage:append($fileName, $arrTest[0]) and then loop over all results.

Is there some easy way to output an entire array's elements on new lines with Storage:put instead of just a single line?

wanna_coder101
  • 508
  • 5
  • 19

2 Answers2

1

Quick answer. You can use implode :

$arrTest = [
  0 => "Val 1",
  1 => "Val 2"
];
$arrTest = implode("\n", $arrTest);

Storage::put('test.log', $arrTest);
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
  • Too bad you can't just have the storage::put method output a new file with lines and need to add \n to the array then convert it into a string. – wanna_coder101 Jan 17 '22 at 04:00
0

Try this method. Hope is helpful.

foreach ($arrTest as $index => $arr) {
    Storage:put($fileName, $arr[$index] + "\n")
}