I have an array $customers, and want to print each value of the array to a text file. Array looks like this:
[0] = Sam, John, Rick
[1] = Jacob, Richard, David
[2] = Jesse, Frank, Louise
This is what I had in mind, but it doesn't seem to like implode:
$pos = 0;
foreach ($customers as $customer)
{
$result = implode(" ",$customers[$pos]);
//echo implode(" ",$customers[$pos]);
file_put_contents('active.txt', $result);
$pos = $pos + 1;
}
The result I would expect in the text file is:
Sam, John, Rick
Jacob, Richard, David
Jesse, Frank, Louise
Can anyone explain how to do this? The goal is to have the array to appear comma delimited in a text file to export to Excel.
file_put_contents()overwrites file contents if theFILE_APPENDflag is not set. You end up with only the last line in your file. Also I suggest not to usefile_put_contents()in a loop because it opens and closes the file every time. This is a lot of I/O operations which take a lot of (unnecessary) time.