DEV Community 👩‍💻👨‍💻

Cover image for How To Print & Write Array Values to Files in Laravel 8
Code And Deploy
Code And Deploy

Posted on

How To Print & Write Array Values to Files in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-print-write-array-values-to-files-in-laravel-8

In my previous post, I shared how to print and write array values in PHP now we will do it with Laravel 8 using the File class provided by Laravel. In this example, we have an animal's array of values. We will loop it and we are using PHP_EOL to new line each result and append it to a variable $contents.

$contents = '';

$animals = ['Cat', 'Dog', 'Carabao', 'Horse'];

foreach ($animals as $animal) {
    $contents .= $animal.PHP_EOL;
}

File::put('filename.txt', $contents, true);
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-print-write-array-values-to-files-in-laravel-8 if you want to download this code.

Happy coding :)

Oldest comments (0)

Join 900k+ Developers on DEV

You'll see more of the content that brought you here.