Page 1 of 1
php text file to array
Posted: Sat Jul 19, 2003 2:26 pm
by oculasm
hi everyone, I'm a bit of a php newbie so any help you offer would be greatly appreciated.
I have a text file with the format below.
Code: Select all
004
This is the fifth message, blah blah etc..
R
15/07/2003
005
This is the fifth message, blah blah etc..
R
16/07/2003
I want to put the contents of this text file into a numerically indexed array where each element is an associative array.
entries array (numerically indexed array)
|
|
|
\___ entry array (associative)
|
|
|
\___ entry array (associative)
|
|
|
\___ entry array (associative)
something like below for each text file entry. but prefferably in a for loop.
$entries[0][entrynum]
$entries[0][entrymsg]
$entries[0][entrycolor]
$entries[0][entrydate]
$entries[1][entrynum]
$entries[1][entrymsg]
$entries[1][entrycolor]
$entries[1][entrydate]
.
.
.
the bit which I can't figure out is how to read a text file's contents into the array I described.
if it doesn't make sense please let me know. any helps, tips/ links, code would be appreciated..thanks

Posted: Sun Jul 20, 2003 2:19 pm
by Slippy
You should convert/create your source file as a CSV file and then parse using fgetcsv:
http://ca3.php.net/manual/en/function.fgetcsv.php
This will eliminate the need for complicated ereg calls on strings.
Posted: Sun Jul 20, 2003 2:33 pm
by oculasm
wow I am quite a newbie but I shall try and look up what up you have mentioned.
Posted: Sun Jul 20, 2003 5:39 pm
by trollll
Um... Or you could just do something like
Code: Select all
$fileContents = file_get_contents("myfile.txt");
$fileParts = explode("\n\n", $fileContents);
$entries = array();
foreach ($fileParts as $entry) {
$tempArray = explode("\n", $entry);
array_push($entries, array("entrynum" => $tempArray[0], "entrymsg" => $tempArray[1], "entrycolor" => $tempArray[2], "entrydate" => $tempArray[3]));
}
Something along those lines should work just fine.
Posted: Sun Jul 20, 2003 6:10 pm
by oculasm
hey trollll. thanks I'll give it a shot tomorrow..
very helpful of you.
regards
-i
Posted: Sun Jul 20, 2003 8:50 pm
by oculasm
based on what trollll said I have this..
Code: Select all
<?php
$fileContents = file("blog.txt");
$fileParts = explode("\n\n", $fileContents);
$entries = array();
foreach ($fileParts as $entry) {
$tempArray = explode("\n", $entry);
array_push($entries, array("entrynum" => $tempArray[0], "entrymsg" => $tempArray[1], "entrycolor" => $tempArray[2], "entrydate" => $tempArray[3]));
}
echo $entries[0]['entrynum'];
?>
it doesnt quite work unfortunately.. it only returns the word Array ??

Posted: Sun Jul 20, 2003 9:08 pm
by oculasm
ps. I couldnt use file_get_contents as I only have
PHP Version 4.1.2
Posted: Sun Jul 20, 2003 9:23 pm
by qartis
it only returns the word Array ??
Instead of the last echo line, try
And show us the output.
Posted: Sun Jul 20, 2003 9:49 pm
by oculasm
the output is
Array ( [0] => Array ( [entrynum] => Array [entrymsg] => [entrycolor] => [entrydate] => ) )
when you view source its like this
Array
(
[0] => Array
(
[entrynum] => Array
[entrymsg] =>
[entrycolor] =>
[entrydate] =>
)
)
Posted: Sun Jul 20, 2003 10:10 pm
by trollll
If you need to use file() instead, then you just need different logic in the loop.
Code: Select all
$fileContents = file("blog.txt");
$entries = array();
for ($i = 0; $i < count($fileContents); $i += 5) {
array_push($entries, array("entrynum" => $fileContents[$i], "entrymsg" => $fileContents[$i + 1], "entrycolor" => $fileContents[$i + 2], "entrydate" => $fileContents[$i + 3]));
}
Somone please double-check my logic for me, but I think that should do it. I think it didn't work before for two reasons. The first, once you read in a file using file() it gets kept in an array, one line per item. Trying to split it won't do a thing except confuse php. The second, I think I made a mistake in the first try in assuming that the array would copy everything over to the new one. Since arrays get passed by reference, that may (may not, but you never know) have just passed the reference and confused php once again.
Posted: Sun Jul 20, 2003 10:33 pm
by oculasm

wow it works.. you're the man!
thank you very very much..
full code is
Code: Select all
<?php
$fileContents = file("blog.txt");
$entries = array();
for ($i = 0; $i < count($fileContents); $i += 5) {
array_push($entries, array("entrynum" => $fileContents[$i], "entrymsg" => $fileContents[$i + 1], "entrycolor" => $fileContents[$i + 2], "entrydate" => $fileContents[$i + 3]));
}
//echo $entries[0]['entrynum'];
//echo ($entries[0][2]);
//print_r($entries);
//print_r($entries[0]['entrynum']); //doesnt work
//print_r($fileContents);//works
//print_r($entries);//works
print_r($entries[0][entrymsg]);
?>
parses it like a treat
Posted: Sun Jul 20, 2003 10:41 pm
by trollll
And once again, the day...is saved. Thanks to...never mind.

Just glad I could help.
Posted: Sun Jul 20, 2003 10:50 pm
by oculasm
yes trolll you certainly saved my day..thanks,
speaking of which its 4:40 am here...
just so you know.. this code is for a tiny blog, a collaboration between me and my friends and a foray into learning some php.
I'd like to credit you for the code in the blog documentation. if you like you could email your name/site address.
I promise I wont bug you if I get stuck..
thanks again to all those who replied.
all the best
iman@oculasm.org