Page 1 of 1

[SOLVED] Make array from contents of a file

Posted: Tue Apr 13, 2004 6:51 pm
by Chris Corbyn
Hi,

How can i make the contents of a file the elements of an array.

For example, I have a file named log.log which might contain this and nothing else
somestring, anotherstring, newstring,
I've tried to make an array where the elements would be $array[0]="somestring" $array[1]="anotherstring" etc etc like this

Code: Select all

$array = array ( include('log.log') );

echo $array[0];
but this echos
somestring, anotherstring, newstring, 1
as what is apparently the first element when it should be just "somestring".

Am I doing this right?

Thanks :-)

Posted: Tue Apr 13, 2004 6:53 pm
by markl999
Try $array = explode(',', file_get_contents('log.log'));

Posted: Tue Apr 13, 2004 6:57 pm
by Chris Corbyn
Yeah that works perfectly. I didn't realise explode() could do that.

Pretty cool. thanks :-)

Posted: Wed Apr 14, 2004 4:09 am
by vigge89
just a reminder, you will not be able to use , in the strings, would make the output wierd :wink: