I have a multidimensional array $csvcontaining the following contents.
Array ( [0] => JOHN WILLIAMS [1] => 6/8/1998 [2] => 55456434E [3] => 4321 )
Array ( [0] => SARAH JONES [1] => 15/01/1982 [2] => 56834645Q [3] => 1234 )
Array ( [0] => JAMES BRENNAN [1] => 09/05/1978 [2] => 25689514W [3] => 8575)
I got this array from a csv file to update one of the elements within the file. Now that its updated, I want to add the updated elements back to a new csv file.
$csv = array();
$lines = file("persistence/employee_data.csv", FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
I want to write this updated array to a new csv file but you cannot write a multidimensional array to a csv file. I want to convert this array to something like this
Array ( [0] => JOHN WILLIAMS [1] => 6/8/1998 [2] => 55456434E [3] => 4321 )
Array ( [0] => SARAH JONES [1] => 15/01/1982 [2] => 56834645Q [3] => 1234 )
Array ( [0] => JAMES Brennan [1] => 09/05/1978 [2] => 25689514W [3] => 8575
)
so I can easily use the fputcsv()function to write the contents of the array to a csv file.
$dataSrc = "persistence/employee_data.csv";
$outFile = fopen($dataSrc, "w") or die("Unable to open file!");
fputcsv($outFile, $csv);
fclose($outFile);
the format within the csv file will look like this (not including the enclosures).
JANE WILLIAMS,6/8/1998,55846874E,4321
SARAH JONES,15/01/1982,56897547Q,1234
JAMES BRENNAN,09/05/1978,25689514W,8575
What would be the best approach to do this?
EDIT
Just simply used a for each statement lol.
$dataDest = "persistence/employee_data.csv";
$outFile = fopen($dataDest, "w") or die("Unable to open file!");
foreach($csv as $value){
fputcsv($outFile, $value);
}
fclose($outFile);