php array file

Discussion in 'PHP' started by arunsuriindia, Mar 23, 2007.

  1. #1
    $list[20][20]

    1)i want to write this array as a csv
    how do i do it

    2) how do i write a 2d array to the file
     
    arunsuriindia, Mar 23, 2007 IP
  2. maonnie

    maonnie Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    1: Using fopen, for, fwrite & fclose ;)

    2: See: php.net/serialize
     
    maonnie, Mar 23, 2007 IP
  3. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Serialize() dumps a string in "serialized PHP" format. OP needs a csv.

    This should do it:

    
    <?
    
    // output file name
    $filename = 'data.csv';
    
    	$file = fopen($filename,'w');
    	foreach ($list as $line)
    		fwrite($file,implode(',',$line) . "\n");
    	fclose($file);
    ?>
    
    PHP:
     
    sea otter, Mar 24, 2007 IP
  4. arunsuriindia

    arunsuriindia Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanx buddy
     
    arunsuriindia, Mar 24, 2007 IP
  5. maonnie

    maonnie Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    He asked 2 questions, 1. how to write a CSV, 2. To write a 2d array to a file, for the latter you'd use serialize.

    Like I said, using fopen, for[each], fwrite & fclose.
     
    maonnie, Mar 25, 2007 IP