PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

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

 February 21, 2022     flysystem, laravel, php, storage     No comments   

Issue

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?


Solution

Quick answer. You can use implode :

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

Storage::put('test.log', $arrTest);


Answered By - Wahyu Kristianto
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing