Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:38:27+00:00 2026-06-04T19:38:27+00:00

There are two arrays that I’d like to save as CSV file from PHP.

  • 0

There are two arrays that I’d like to save as CSV file from PHP. The problem is that this code works only for the first Call. E.g. in the below-given example I can save only array1, but array2 is not saved. If I swap places of array1 and array2, then array2 will be saved instead of array1. What’s actually wrong in my code and how could I solve this problem?

     header("Content-type: text/csv");
     header("Pragma: no-cache");
     $headers = array('xxx','yyy','zzz');
     saveCSV($array1,"file1.csv",$headers);

     header("Content-type: text/csv");
     header("Pragma: no-cache");
     $headers = array('aaa','bbb','ccc');   
     saveCSV($array2,"file2.csv",$headers);

function saveCSV($data,$fileName,$headers) {
    $outstream = fopen($fileName, "a");
    file_put_contents($fileName, "");

    if ($headers != 0)
        fputcsv($outstream,$headers);

    function __outputCSV(&$vals, $key, $filehandler) {
        fputcsv($filehandler, $vals);
    }
    array_walk($data, "__outputCSV", $outstream);
    fclose($outstream);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:38:28+00:00Added an answer on June 4, 2026 at 7:38 pm

    Your setting the headers, twice.

    Trying building up your csv data output, then once completed set the headers once.

    Also you have a function declaration within another, which i’m sure is invalid.

    move __outputCSV somwehere else, or maybe you intended it to be a closure?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays that are structured like this $array1 = Array ( [0]
There are two arrays, that are given for $link from foreach . One time
I currently have two arrays that look like this: Swatches: Array ( [0] =>
I'm new to boost shared arrays. There is existing code that declares two arrays:
I have a NSDictionary with two arrays like this questionAndAnswer= [NSDictionary dictionaryWithObject:answerList forKey:questionList]; there
Is there an easy way to check in a unit test that two arrays
I have two arrays of values like X,Y,Z and 1,2 There is a table
Is there a built-in function for PHP for me to check whether two arrays
I have two arrays of values that I would like to combine - but
I have two associative arrays in PHP that are defined as following: $this_week[] =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.