Click to See Complete Forum and Search --> : Array() help needed!


deep
08-23-2003, 04:09 PM
Hello!

I got a file named toplist.txt. The text in this file is placed on each and every line like this:

Line1: Volvo
Line2: Saab
Line3: Nissan

Now I need to open this file and extract every line in an array.
It should look like this:

$array[0] = "Volvo";
$array[1] = "Saab";
$array[2] = "Nissan";

How do I do that?

Thanks!

pyro
08-23-2003, 07:15 PM
I'm assuming that the Line 1: Line 2: etc, are not there in the actual file...

So, all you need to do is read the file using file() (http://www.php.net/file), and it will automatically put each line into an array.

$file = file("toplist.txt");
foreach ($file as $car_make) { #loop through the array
echo $car_make."<br>";
}

Kr|Z
08-24-2003, 07:13 AM
Note that when using file(), it will read the lines with the following linebreaks. So it will return:

$array[0] = "Volvo\n";
$array[1] = "Saab\n";
$array[2] = "Nissan";

You can use rtrim() to remove this