vorandasb
vorandasb

Reputation: 23

How can I insert array data to a file?

I have created two arrays and I want all the information from both to save to a .txt file. I want it to save in the format ('DATE' 'RAINFALL') inside the file. I tried many sources for help and I still can't get it to save. How can I do it? Thank you

The format inside the txt file

This is the format inside the .txt file I want

Code I currently have:

$rainf_array = array($rainf0, $rainf1, $rainf2, $rainf3, $rainf4, $rainf5, $rainf6 );

    $date_array = array($date0, $date1, $date2, $date3, $date4, $date5, $date6 );


   //Input value is saved to the file

   {
     $fileHandle = fopen($fileName, "w");

      fwrite($fileHandle, $date_array .'' . $rainf_array . ",\n" );


     fclose($fileHandle);
   }

Upvotes: 0

Views: 550

Answers (1)

AbsoluteBeginner
AbsoluteBeginner

Reputation: 2255

My suggestion:

$date_array = array($date0, $date1, $date2, $date3, $date4, $date5, $date6);
$rainf_array = array($rainf0, $rainf1, $rainf2, $rainf3, $rainf4, $rainf5, $rainf6);

$combined = array_combine($date_array, $rainf_array);

foreach ($combined as $key => $value) {
  $input .= $key . ' ' . $value . "\n";
}

Then save $input into the file.

Upvotes: 1

Related Questions