How you fix things depends very much on what you want to do with the stored data.
The simple example is to write each element separately:
<?php
$arr = ['Apple','Orange','Lemon'];
$fh = fopen('myFile.csv', 'w');
foreach($arr as $el) {
fwrite ($fh, "$el\n"); // add a new line after each element to delimit them
}
fclose($fh);
You could create a CSV file with fputcsv()
:
<?php
$arr = ['Apple','Orange','Lemon'];
$fh = fopen('myFile.csv', 'w');
fputcsv($fh, $arr);
fclose($fh);
You could write JSON data:
<?php
$arr = ['Apple','Orange','Lemon'];
file_put_contents('myFile.json', json_encode($arr));
If you're feeling bold you could create an XML file (no snippet for this one, but trust me, it can be done).
You could write serialized PHP data (see serialize()) - not recommended.
Or you could create your own format for your specific application.