php text file to array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

php text file to array

Post 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 :)
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post 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.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

wow I am quite a newbie but I shall try and look up what up you have mentioned.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

hey trollll. thanks I'll give it a shot tomorrow..
very helpful of you. :D


regards
-i
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post 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 ?? 8O
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

ps. I couldnt use file_get_contents as I only have
PHP Version 4.1.2
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

it only returns the word Array ??
Instead of the last echo line, try

Code: Select all

print_r($entries);
And show us the output.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post 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] =>
)

)
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

:D 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
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

And once again, the day...is saved. Thanks to...never mind.

:D Just glad I could help.
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post 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.. :lol:

thanks again to all those who replied.
all the best

iman@oculasm.org
Post Reply