Please help.
I have the following data file (Let's call it data.txt):
201200|0.00470|0.01480|0.00720|0.00400
201202|0.03490|0.01960|0.01190|0.00861
201203|0.01429|0.03370|0.01245|9.99999
201204|0.00490|0.01560|0.01590|0.00861
201205|0.01490|0.02370|0.03999|0.04234
Here is my php script (Let's call it script.php):
<?php
$filename = $argv[1];
$array = file($filename);
foreach ($array as $line) {
list($x,$a,$b,$c,$d) = explode('|',$line);
$array2 = array("A"=>$a,"B"=>$b,"C"=>$c,"D"=>$d);
asort($array2);
reset($array2);
while (list($key,$val) = each($array2)) {
echo "|$key|$val";
}
echo "\n";
}
?>
When I ran: #php script.php data.txt
I got the following result:
|D|0.00400
|A|0.00470|C|0.00720|B|0.01480
|D|0.00861
|C|0.01190|B|0.01960|A|0.03490
|C|0.01245|A|0.01429|B|0.03370|D|9.99999
|A|0.00490|D|0.00861
|B|0.01560|C|0.01590
|A|0.01490|B|0.02370|C|0.03999|D|0.04234
I wonder whether anyone know why my result did not print line by line. I was expecting the result as follows:
|D|0.00400|A|0.00470|C|0.00720|B|0.01480
|D|0.00861|C|0.01190|B|0.01960|A|0.03490
|C|0.01245|A|0.01429|B|0.03370|D|9.99999
|A|0.00490|D|0.00861|B|0.01560|C|0.01590
|A|0.01490|B|0.02370|C|0.03999|D|0.04234
It seems to me that when php create an array; it also creates some extra new line.
Please advise.
Thank you,
Dorn.