chris22 Posted March 25, 2011 Share Posted March 25, 2011 I have the following code: <?php $lines = count(file("input.txt")); $file = fopen("input.txt", "r"); for($i=0; $i<=$lines; $i++) { $friend = fgets($file); $friendInfo = explode(" ", $friend); $friendID[$i] = $friendInfo[0]; $friendLAT[$i] = $friendInfo[1]; $friendLONG[$i] = $friendInfo[2]; echo $friendInfo[2]; echo $friendLAT[$i]; } fclose($file); ?> The value of $friendInfo[2]; and $friendLAT[$i]; echo out alright. However I get the following output: 0.0 0.0-10.1 10.112.2 -12.238.3 38.3179.9979.99 Notice: Undefined offset: 1 in C:\inetpub\wwwroot\test.php on line 18 Notice: Undefined offset: 2 in C:\inetpub\wwwroot\test.php on line 19 Notice: Undefined offset: 2 in C:\inetpub\wwwroot\test.php on line 21 It doesn't make any sense to me, I can assign the value of the array to a variable ($friendLAT[$i], but it says the offset is undefined. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 25, 2011 Share Posted March 25, 2011 What does the input file look like? Ken Quote Link to comment Share on other sites More sharing options...
aabid Posted March 25, 2011 Share Posted March 25, 2011 I just checked the code and every thing seems to correct at my end. The line I stored in input.txt was "This is just a test." and it ran correctly at my end. May be there something with content of the file you are trying to read. Quote Link to comment Share on other sites More sharing options...
chris22 Posted March 25, 2011 Author Share Posted March 25, 2011 1 0.0 0.0 2 10.1 -10.1 3 -12.2 12.2 4 38.3 38.3 5 79.99 179.99 ^^ Contents of input file. Quote Link to comment Share on other sites More sharing options...
sasa Posted March 25, 2011 Share Posted March 25, 2011 are you shure that your file doesn't end with blank line? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 25, 2011 Share Posted March 25, 2011 I looked at your code and the following might be a better way of doing what I think you're trying to do... <?php $file = file("input.txt"); $friends = array(); foreach ($file as $line) { $friend = trim($line); $friendInfo = explode(" ", $friend); $friends[$friendInfo[0]] = array('lat'=>$friendInfo[1],'long'=> $friendInfo[2]); } print_r($friends); ?> Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.