phpRoshan Posted May 20, 2007 Share Posted May 20, 2007 Hi evrybody i wonder hw to get an entire file data into one array and in that array how to arrang those filedata into elements? Any suggestions? any idea? Tnx ??? Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/ Share on other sites More sharing options...
trq Posted May 20, 2007 Share Posted May 20, 2007 Your question is pretty vague.... maybe? <?php $array = file('path/to/file'); ?> Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257442 Share on other sites More sharing options...
phpRoshan Posted May 20, 2007 Author Share Posted May 20, 2007 yes. sory abt that. what i ment was how to put the contents/words of the file in to array elements? like arr[0] = 'some word' arr[1] = 'some othe word' arr[2] = 'maybe a digit or digits?' in this way. i dont thing if i use "$array = file('path/to/file');" it wont do that? does is? :-\ Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257452 Share on other sites More sharing options...
trq Posted May 20, 2007 Share Posted May 20, 2007 The file function saves each line from a file into an array element. Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257456 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 file() gives you an array containing each line in its own key. You can then split file() up, eg: <?php $filename = "path/to/file"; $lines = file($filename); foreach($lines as $linenumber => $linedata) { $words[$linenumber] = explode(" ", $linedata); } ?> Now you have an array ($words) with sub arrays containing the words in each line of the file found in $filename. Not tested, but you get the idea Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257457 Share on other sites More sharing options...
neel_basu Posted May 20, 2007 Share Posted May 20, 2007 Whats the format of the data on your file ?? Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257461 Share on other sites More sharing options...
kucing Posted May 20, 2007 Share Posted May 20, 2007 Try this way.. $filename = "path/to/file"; $data = file($filename); function fileToArray($data) { $words = explode(' ',$data); foreach ($words as $word) { $outputArray[]=$word; } print_r($outputArray); } fileToArray($data); Not tested yet but seems to be working in my mind.. lol Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257465 Share on other sites More sharing options...
neel_basu Posted May 20, 2007 Share Posted May 20, 2007 A little change on the above code $filename = "path/to/file"; $data = file($filename); function fileToArray($data) { $words = preg_split('/\s+/',$data); foreach ($words as $word) { $outputArray[]=$word; } print_r($outputArray); } fileToArray($data); Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257470 Share on other sites More sharing options...
phpRoshan Posted May 20, 2007 Author Share Posted May 20, 2007 Thank you so much for ur contribution of ideas. I learnt a lot more than i expected. Link to comment https://forums.phpfreaks.com/topic/52199-solved-how-to-get-entire-file-data-into-a-single-array/#findComment-257612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.