0

I have looped through an array and can successfully print out the rows that I need to work with

foreach($array['Stock'] as $row) {
    echo '|' . $row['Code'] . '|' . intval($row['Onhand']) . "\r\n";
}

I need to have all of the printed rows to be saved in a csv file. Should I be converting to an array first and then creating a csv or can I do it from within the loop?

Just to clarify guys, the file requirements are

'This CSV file must contain 3 columns, separated by the pipe symbol (|), without column names. '

1
  • it looks like you are delimiting each row with a pipe | for a csv I would have expected to see a comma used. Will add an answer but could you clarify this please Commented Jun 23, 2016 at 10:32

1 Answer 1

3

Prepend each row onto a variable like:

$csv .= $row['Code'] . ',' . intval($row['Onhand']) . "\r\n";

Then use file_put_contents to save to local file, or use header to output headers to force download to browser.

PS: CSV stands for comma separated values, not pipe separated values, so if I'm correct in assuming you're using the pipe | to separate values it's best to use a comma instead.

Sign up to request clarification or add additional context in comments.

1 Comment

damn beat me to it +1 for the comma seperated values as opposed to pipe

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.