1

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);
Note I know how to create and write to a new file, I just need help on the multidimensional array problem converting it into something that I can easily write to a csv file.

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);

2 Answers 2

1

As you figured, fputcsv() takes a unidimensional array as input, and saves each element in one row.

To save each array in an array, loop through the parent array and save each subarray as you would normally with fputcsv():

<?php
$data[] = ["JANE WILLIAMS", "6/8/1998", "55846874E", "4321"];
$data[] = ["SARAH JONES", "15/01/1982", "56897547Q", "1234"];
$data[] = ["JAMES BRENNAN", "09/05/1978", "25689514W", "8575"];

$outFile = fopen("data.csv", "w") or die("Unable to open file!");
foreach ($data as $row) {
    fputcsv($outFile, $row);
}
fclose($outFile);
Sign up to request clarification or add additional context in comments.

Comments

1

fputcsv() writes a single one-dimensional array to the file pointer. You need to loop through your two-dimensional array and call it for each of the sub arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.