php - Foreach loop printing duplicate last entry with output to data file -


i have foreach loop loops through present html , store data data file. issue (which has been raised before) last alliteration of foreach loop duplicated. using unset not work, writes data file within foreach loop! how can fix issue? want cut out duplicate , maintain writing data file...

my code:

// save data dat file $datafile = "datfile.dat"; $fh = fopen($datafile, 'w') or die("can't open file");  foreach($xml->drop $shipment) {     $connote_id = $shipment->header->bill_of_lading; // 20 chars     $output = sprintf("%-20.20s", $connote_id);     echo $output;     unset($shipment);     fwrite($fh, $output); }  $output .= "%%eof"; fwrite($fh, $output); echo "%%eof"; fclose($fh); 

xml:

<?xml version="1.0" encoding="utf-8"?> <ishpmnt1> <drop> <header>   <bill_of_lading>vw038687030000006</bill_of_lading> </header> </drop> </ishpmnt1> 

the issue resided understanding how fwrite works. writing last foreach 'output' variable twice, second adding %%eof hence why seemed duplicate sent through foreach loop.

fixed coding section:

echo $output; fwrite($fh, $output); } fwrite($fh, "%%eof"); 

thanks tried help! appreciated


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -