# PHP Iterating a file and assigning variables



## scrubbicus (Jan 23, 2009)

Hey!

I've been trying so many different ways, many of them ending up infinite iterations and my computer freezing but what I'm trying to do is open a file and get each line from it in a loop. While it's looping, each line will be assigned to a variable, or preferably an array. Like this example.

include.txt
{Name: Here, E-Mail: Here, Location: Here}

then I want it to iterate that line and implode it with the commas being seperaters into an array like this

Array (0 => Name: Here, 1=> E-Mail: here, 2=> Location: Here)

Any help would be appreciated. Thanks.


----------



## MMJ (Oct 15, 2006)

http://php.net/file


----------



## Eriksrocks (Aug 7, 2005)

For the second part:
http://www.php.net/explode


----------



## MMJ (Oct 15, 2006)

Eriksrocks said:


> For the second part:
> http://www.php.net/explode


file() already does that for you. 


[URL]http://php.net/file[/URL] said:


> Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.


----------



## Eriksrocks (Aug 7, 2005)

From how I read his post he wanted to first split up the file into an array with each line being an element (which is accomplished with file()), and then he wanted to explode each line (element of the first array) into a seperate array with commas as separators.

So you might have a file like this:


```
Name: one, E-Mail: one, Location: one
Name: two, E-Mail: two, Location: two
Name: three, E-Mail: three, Location: three
```
Then at the end of the day you would end up with something like:

$firstarray[0] is equal to "Name: one"
$secondarray[1] is equal to "E-Mail: two"
$thirdarray[2] is equal to "Location: three"

That is assuming I understood the original post correctly.


----------



## scrubbicus (Jan 23, 2009)

So when the user submits the form , for example. They'll fill in these fields. Name, E-Mail and Location. The form information will be collected and appended to file.text. Then I'll have another script which I need help on that will collect the information from file.text, each line on file.text will be an individually submitted form with Name, E-Mail, and Location of each individual on the same line. The script will open file.txt and iterate through each line. At the end of the day I'll have one array for each individuals information.

$firstarray(name, email, location)
$secondarray(name, email, location)
$thirdarray(name, email, location)

and so on.

if possible, which would be awesome would be

$firstarray(
name => 'name',.
email => 'email',
location => 'location'
);

and so on... thanks ill take a look at those examples on php.net, I looked pretty throughoughly throughout their website on information on while and for loops as well as the file() information.


----------



## Eriksrocks (Aug 7, 2005)

Enjoy. (Fully tested. )


```
<?php

$lines = file("file.txt", FILE_IGNORE_NEW_LINES);
foreach( $lines as $key => $value )
{
	$temparray = explode( ", ", $value);
	$name = "array";
	${$name.$key} = array( "name" => $temparray[0], "email" => $temparray[1], "location" => $temparray[2] );
}

?>
```
EDIT: A bit more explanation: This will create as many arrays as there are lines in the file. Each array will be "keyed" as you described. So for example $array2["email"] would be something like "[email protected]" (note that $array2 would actually be the third line in the file because arrays start at 0).


----------



## scrubbicus (Jan 23, 2009)

thanks! i was figuring it would be a foreach loop and i was wondering if you could set a variable to a variable as in $($) but after i froze my php program a few times after looping infinitely i didnt want to mess around anymore haha.

so im looking over it and im confused why $key and $value get both the values that they get. especially why $key gets the number of the line.


----------



## Eriksrocks (Aug 7, 2005)

Well in the $lines array the elements consist of the lines of the file. So $lines[0] would contain the first line.

Are you familiar with how foreach works? Basically it's just going through each element of $lines and assigning the value to $value and the key to $key.

So in the case of $lines[0] then $key would be 0 and $value would be the contents of the first line. Does that make sense?


----------



## MMJ (Oct 15, 2006)

Since the example you posted is valid JSON you can try PHP's JSON functions.


----------



## scrubbicus (Jan 23, 2009)

ya i'm familiar with a foreach loop. Usually I use them like one of this.

$array = array(
'username' => 'name',
password' => 'password'
);

foreach($array as $option => value) {
echo "You're $option is $value"
{

or

$numbers = array(1, 2, 3, 4, 5);

foreach($numbers as $num) {
echo $num;
}

so the values and keys are already there in the array and i can tell where they came from but in the foreach that you wrote I can tell where the keys came from since their in the other document seperated by a comma but i cant tell where the number came from since it wasn't previously in the document so i can only guess its where the line is in the document like the first line would be 0, second line would be 1 and so on but im just confused how PHP automatically grabbed those values and how I was presume something like that for the future.


----------



## Eriksrocks (Aug 7, 2005)

Both the keys and values come from the file() function which automatically creates the array $lines. The keys are enumerated like a regular array where 0 is the first element 1 is the second and so forth. The values come from the file() function separating the file given into individual lines. 

I don't know how else to explain it.


----------



## scrubbicus (Jan 23, 2009)

ok! that makes more sense. i did not know that the file() function creates arrays i thought that just called the file so the foreach loop could do what it does. i thought it was kind of like a fopen() function but with a different name. cool stuff thanks.


----------

