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 :

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 :


In the above, we could replace:

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


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


file -- Reads entire file into an array

II. Array Functions

Was this answer helpful?

 Print this Article

Also Read

How to send email using PHP script?


How can I call a command line executable from within PHP?

Check out the "Program Execution functions" here :...

Is it possible to have MySQL and PHP run with one provider and the "database driven website" with the other ? Say with the help of only SSI / CGI?

Yes. Let your CGI/SSI include from the urls on your other site. On yourother site you return...

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...

How to redirect from php page to another php page?

For redirecting, you can use the PHP header() function like so : header("Location:...