I'm unable to write array to text file in new lines. My code:
echo '<pre>';
print_r($final_result);
echo '</pre>';
Output:
Array
(
[0] => Something1
[1] => Something2
[2] => Something3
)
Then:
file_put_contents($file, trim($final_result . "\n"), FILE_APPEND);
Output:
Something1Something2Something3Array
My goal is:
Something1
Something2
Something3
Any ideas? :)
Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.
The file_put_contents() function in PHP is an inbuilt function which is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.
How about
file_put_contents($file, implode(PHP_EOL, $final_result), FILE_APPEND);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With