I'm looking for how to save the content of an array in a.txt:
<?php
$resul = array('hermaniribeiro', 'Ebtsama', 'BrittoOFC', 'CheesterG', 'dsolutec', 'ExpoGanSon', 'dsolutec', 'ExpoGanSon', 'dsolutec', 'BelforFx', 'kunakrec', 'YouTube', 'Dasabuvir', 'greentechmedia', 'bardomsw', 'MdeMotion', 'iAnonymous', 'WilliamCorvera', 'MadridVen', 'Bertty17', 'SoyBobMarley', 'joseapontefelip', 'la_patilla', 'hootsuite', 'fawkestar70', 'starwars');
$file = fopen("user.txt", "a");
foreach($resul as $final){
fwrite($file, PHP_EOL ."$final");
}
fclose($file);
?>
I try it that way but it only prints the result but does not save it and what I am looking for is that it saves it the same as it prints it with line break.
Good,
An array is a dynamic object class, so it would not serve you well to hold static content (without server-side intervention). However, you can use a serialization language such as JSON.
Use
json_encode()
to perform the save, and to avoid confusion I suggest you change the filename to extension.json
If that's not the case and you just want to host some content quickly you can use a loop like
foreach
I hope I have been of help to you and have a nice night.
The open-write-only option of
'w'
fopen ()fopen("user.txt", "w");
will truncate the previous content, so that at the end of the iteration you will only have the last value of the array in the file.Two important things, for each iteration it would be opening the file (fopen) and closing (fclose)
user.txt
, which is wrong should be outside thebucle
, also the end of line would go after the value (for more consistency if not your file will start with a jump of line) .I HAVE TRIED THIS EXAMPLE, BUT WHY DOES IT ADD A BLANK LINE AT THE END?