Reputation: 39
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.
Upvotes: 0
Views: 140
Reputation: 57121
Each element of $customers
is a single value, so rather than using a loop and imploding the strings, which also overwrite each other in the file.
You can just implode()
the whole array with PHP_EOL
for the line separator...
file_put_contents('active.txt', implode(PHP_EOL ,$customers));
Upvotes: 2