rbrown Posted March 1, 2008 Share Posted March 1, 2008 I'm trying to open a file which is saved as the first line is the key the second line is the value and so on... When I loop through it I keep getting the keys and values screwed up. It either returns the key and the value and then the value is the next key Or it returns each key and value as a value. Thanks, Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 <?php $lines = file('file.txt') { $tmp = array(); for ($i=0;$i<count($lines);$i++) { $tmp[$lines[$i]] = $lines[$i++]; } print_r($tmp); ?> Quote Link to comment Share on other sites More sharing options...
KrisNz Posted March 1, 2008 Share Posted March 1, 2008 <pre> <?php $contents = array_chunk(file("data.txt"),2); $final = array(); foreach ($contents as $array) { $final[rtrim($array[0])] = $array[1]; } print_r($final); //or $contents = file("data.txt"); $size = count($contents); $final = array(); for ($i=0;$i<$size;$i++) { $final[rtrim($contents[$i])] = $contents[++$i]; } print_r($final); ?> Quote Link to comment Share on other sites More sharing options...
rbrown Posted March 1, 2008 Author Share Posted March 1, 2008 KrisNz... Perfect!!! Thanks! thorpe... Just made the key and value the same. Thanks Guys!!! 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.