[SOLVED] Make array from contents of a file

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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

[SOLVED] Make array from contents of a file

Post 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 :-)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try $array = explode(',', file_get_contents('log.log'));
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah that works perfectly. I didn't realise explode() could do that.

Pretty cool. thanks :-)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

just a reminder, you will not be able to use , in the strings, would make the output wierd :wink:
Post Reply