DEV Community

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

Posted on • Updated 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

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

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

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

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 :)

Latest comments (0)