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
http://www.php.net/manual/control-structures.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

  • 19 Users Found This Useful
Was this answer helpful?

Related Articles

I get an error 'Server.CreateObject Failed' when I try to use CDONTs. What can I do?

CDONTs is no longer supported on Windows 2003 server.  Microsoft introduced CDO back several...

Do you allow custom COM components?

Yes, we do allow Custom Components and we have to charge setup fee for your COM Components...

ASP to MSSQL connection

Before you can access your MS-SQL Server database from your ASP code , you need to connect to it...

How do I send email from ASP using SMTP Authentication?

Please note that our mail server is configured with Authentication. Below is the code snippet...

After I configure the custom error setting in the control panel, I still get the generic error page?

Custom error setting is a web server setting that sets your website Error Pages, but you can...