How can I take a list and make each row a value in an array?

If you have a text file that looks like this :

john|orange|cow
sam|green|goat
mick|red|dragon
Each line can magically be an element in the array using the PHP file()
function, like so :

$lines = file('info.txt');

print $lines[0]; // john|orange|cow
print $lines[1]; // sam|green|goat
print $lines[2]; // mick|red|dragon

Using explode() will help seperate the lines by the seperator, which in
this case is a '|' , the following will loop through the text file,
explode each line and print out the given parts. We're assuming that
the text file (info.txt) has this format :

name|color|animal
name|color|animal
name|color|animal



In the above, we could replace:

$p = explode('|', $line);

With:

list($name, $color, $animal) = explode('|', $line);

To create/define more _friendly_ variables to play with.

Related manual entries are as follows :

explode -- Split a string by string
http://www.php.net/manual/function.explode.php

foreach
http://www.php.net/manual/control-structures.foreach.php

file -- Reads entire file into an array
http://www.php.net/manual/function.file.php

II. Array Functions
http://www.php.net/manual/ref.array.php

Was this answer helpful?

 Print this Article

Also Read

I used PHP for one of the web sites I would like to move over to your hosting service. Is PHP fully supported and (if so) what versions of PHP are supported?

Yes, PHP is fully supported and we do support PHP 5.

I'm new to PHP, where should I start?

Well, try this.The official PHP web site as a lot of useful information:   ...

What is the essence of PHP encryption(using crypt(),mcrypt(),etc)if data on transit isn't safe?

The apathetic response is, of course, "there is no point." If you allowed a login over normal...

I'm new to PHP, and would like to know if there is a code snippet for emailing a lost password to a subcriber

First, we must assume that you are currently storing the username/login, password and the email...

Why i couldn't fetch my variable from the form?

Register_Globals directive is set to OFF because of potential security issues.  PHP has this...