Skip to content
Advertisement

How to output array with new line separated values using Laravel Storage:put

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?

Advertisement

Answer

Quick answer. You can use implode :

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

Storage::put('test.log', $arrTest);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement