Search code examples
phparraysfwrite

Write data to TXT file


I need to write some data into the txt file to be able to open it in MS Excel and get each value in a separate cell. How to do this?

$file = "outputs.txt";
$handle = fopen($file, 'w');
$data = "";
for ($i = 0; $i < $gener; $i++) {
      $data = $data + $i + "; ";
      for ($j = 0; $j < $size; $j++)
      {
        $data = $data + $set->getEntry($j)->getValue(0) + 
                ", " + $set->getEntry($j)->getValue(1) + "; ";
      }
      $data = $data + "\n";
      fwrite($handle, $data); 
}

Solution

  • I would generate a .csv file and then open it with excel.

    You can generate it using this function http://php.net/manual/en/function.fputcsv.php and then you can open the .csv file in excel.