Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    export array values to csv ??

    Hello forums !!
    i would like to know how to export the data in .csv format ?
    suppose i had an array holding values:
    PHP Code:
    $data_arr[$i][0] = "xx";
    $data_arr[$i][0] = "xx";
    $data_arr[$i][0] = "xx";
    $data_arr[$i][0] = "xx"
    where $i varies from 0 to n

    I would like to export this data in .csv format,
    how could this be accomplished ?
    Thanks in advance to all of you

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Have a look at the for() or foreach() loops, and the fputcsv() function.

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Also, I believe that fputscsv only works with PHP 5...

    if you don't have access to that, something like this should work

    PHP Code:
    //make column headings (if desired)
    $contents "col1,col2,col3,col4,etc\r\n";
    $fh fopen("file.csv","w");
    fwrite($fh,
    $num count($data) - 1;
    for(
    $i=0;$i<=$num;$i++)
    {
      
    $contents .= $data[$i][0].",";
    }
    $contents .= $data[$num][0]."\r\n";
    $fh fopen("file.csv","w");
    fwrite($fh,$contents);
    fclose($fh); 
    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •