Reading a text file into variables<

I have a text file of several parameters that I wish to plot using JpGraph. The JpGraph bit I am fine with - I am having problems getting the graph data from the text file into variables.

The text file looks like this:


% range rho-model rho-hs rho-air phi-model phi-hs phi-air
.200E+03 .864E-08 .864E-08 .703E-11 .537E-08 .537E-08 .141E-10
.400E+03 .859E-09 .858E-09 .879E-12 .862E-09 .863E-09 .176E-11
.600E+03 .184E-09 .184E-09 .260E-12 .275E-09 .275E-09 .521E-12
.800E+03 .548E-10 .547E-10 .110E-12 .108E-09 .108E-09 .220E-12
.100E+04 .208E-10 .206E-10 .562E-13 .470E-10 .470E-10 .112E-12
.120E+04 .965E-11 .947E-11 .325E-13 .217E-10 .216E-10 .651E-13
.140E+04 .515E-11 .494E-11 .205E-13 .104E-10 .104E-10 .410E-13
.160E+04 .298E-11 .276E-11 .137E-13 .521E-11 .512E-11 .275E-13
.180E+04 .180E-11 .159E-11 .964E-14 .267E-11 .259E-11 .193E-13
.200E+04 .112E-11 .926E-12 .703E-14 .141E-11 .134E-11 .141E-13
.220E+04 .712E-12 .544E-12 .528E-14 .761E-12 .711E-12 .106E-13
.240E+04 .458E-12 .321E-12 .407E-14 .420E-12 .384E-12 .814E-14
.260E+04 .297E-12 .191E-12 .320E-14 .237E-12 .212E-12 .640E-14
.280E+04 .194E-12 .114E-12 .256E-14 .137E-12 .120E-12 .512E-14
.300E+04 .127E-12 .685E-13 .208E-14 .805E-13 .693E-13 .417E-14
.320E+04 .834E-13 .419E-13 .172E-14 .486E-13 .416E-13 .343E-14
.340E+04 .551E-13 .260E-13 .143E-14 .301E-13 .257E-13 .286E-14
.360E+04 .365E-13 .164E-13 .121E-14 .191E-13 .165E-13 .241E-14
.380E+04 .244E-13 .106E-13 .102E-14 .124E-13 .108E-13 .205E-14
.400E+04 .164E-13 .690E-14 .879E-15 .816E-14 .718E-14 .176E-14
.420E+04 .111E-13 .454E-14 .759E-15 .542E-14 .477E-14 .152E-14
.440E+04 .760E-14 .296E-14 .660E-15 .360E-14 .313E-14 .132E-14
.460E+04 .525E-14 .190E-14 .578E-15 .238E-14 .202E-14 .116E-14
.480E+04 .367E-14 .118E-14 .508E-15 .159E-14 .131E-14 .102E-14
.500E+04 .258E-14 .697E-15 .450E-15 .110E-14 .888E-15 .900E-15
.520E+04 .184E-14 .398E-15 .400E-15 .821E-15 .683E-15 .800E-15
.540E+04 .132E-14 .244E-15 .357E-15 .685E-15 .610E-15 .714E-15



What I need is for all the first column to be put into an array (range), then all the second column put into an array (rho_model) etc etc.

So, in my example, the first two values of the array 'range' will be (.200E+03 and .400E+03), thus:

range[0] = .200E+03
range[1] = .400E+03

How can I make sure the numbers from each column go into the correct array? I initially used the function explode:


<?php
if( !($handle = fopen( $file, "r" ) ) ) {
print "Cannot open the file ($file) for reading!";
exit;
}
while ( !feof( $handle ) ) {
$contents = fread( $handle, filesize( $file ) ) ;
$values = explode ( " ", $contents ) ;
print "$values[0] $values[1]<br/>\n";
}
fclose( $handle );
?>



The print command produces the following on a web page:


% range


All this does is put ALL the values into the same array called $values.

Can anyone help me out? I can clarify if what I wrote is not clear enough. Thanks,

- TatlarOkay, I can now get each LINE from the text file into a seperate array, but not each column....

Code is:


if( !($handle = fopen( $file, "r" ) ) ) {
print "Cannot open the file ($file) for reading!";
exit;
}

$contents = file($file) ;
foreach ($contents as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br>\n";
}

fclose( $handle );



This gives the following output:


Line #0 : % range rho-model rho-hs rho-air phi-model phi-hs phi-air
Line #1 : .200E+03 .864E-08 .864E-08 .703E-11 .537E-08 .537E-08 .141E-10
Line #2 : .400E+03 .859E-09 .858E-09 .879E-12 .862E-09 .863E-09 .176E-11
Line #3 : .600E+03 .184E-09 .184E-09 .260E-12 .275E-09 .275E-09 .521E-12
Line #4 : .800E+03 .548E-10 .547E-10 .110E-12 .108E-09 .108E-09 .220E-12
Line #5 : .100E+04 .208E-10 .206E-10 .562E-13 .470E-10 .470E-10 .112E-12
Line #6 : .120E+04 .965E-11 .947E-11 .325E-13 .217E-10 .216E-10 .651E-13
Line #7 : .140E+04 .515E-11 .494E-11 .205E-13 .104E-10 .104E-10 .410E-13
Line #8 : .160E+04 .298E-11 .276E-11 .137E-13 .521E-11 .512E-11 .275E-13
Line #9 : .180E+04 .180E-11 .159E-11 .964E-14 .267E-11 .259E-11 .193E-13
Line #10 : .200E+04 .112E-11 .926E-12 .703E-14 .141E-11 .134E-11 .141E-13


Ideas? Thx...$contents = file($file);
array_shift($contents);
$range = array();
$rho_model = array();
foreach($contents as $c) {
$c = explode(" ", $c);
$range[] = $c[1];
$rho_model[] = &c[2];
}


that should work :)Thanks n8thegreat.

I am testing that out now. BTW, what does the '&' in front of the '=' on the last line do?



foreach($contents as $c) {
____$c = explode("___", $c);
____$range[]____= $c[1];
____$rho_model[] = &c[2];
}n8thegreat - I am assuming the '&' was an error :P

I have got it working using the following:


foreach( $contents as $c ) {
$c = explode( " ", $c ) ;
$range[] = $c[0] ;
$rho_model[] = $c[1] ;
$rho_hs[] = $c[2] ;
$rho_air[] = $c[3] ;
$phi_model[] = $c[4] ;
$phi_hs[] = $c[5] ;
$phi_air[] = $c[6] ;
}


I can also remove the first line of the text file so I don't have to worry about the column titles.
Thanks for your help - I really appreciate it :)

Happy holidays...oh! sorry! yeah, it was supposed to be a $. should have tested it after i added that line XD
 
Back
Top