Jump to content

declaring variables using a for loop.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
I'll try to make it as simple as possible; I have 3 text files I want to read into 3 different arrays. What I have right now will read them but overwrite the same array:

for($i=0;$i<=2;$i++){

	$filename = "accounts/".$_SESSION['login']."/character".$i".txt";

	if(file_exists($filename)){

		$file = fopen($filename, 'r');

		$characterFile = fread($file, filesize($filename));

		$characterLines = explode("\n", $characterFile);

		fclose($file);

	}

}

I've tried the following and some variants but none will work:

for($i=0;$i<=2;$i++){

	$filename = "accounts/".$_SESSION['login']."/character".$i".txt";

	if(file_exists($filename)){

		$file = fopen($filename, 'r');

		$characterFile[COLOR="Red"].$i[/COLOR] = fread($file, filesize($filename));

		$characterLines[COLOR="Red"].$i[/COLOR] = explode("\n", $characterFile[COLOR="Red"].$i[/COLOR]);

		fclose($file);

	}

}


The result I'm trying to obtain is something like:

$characterLines0 = ["file0line0","file0line1","file0line2"];
$characterLines1 = ["file1line0","file1line1","file1line2"];
$characterLines2 = ["file2line0","file2line1","file2line2"];

I'd validate stuff in a similar manner I believe but I'm lost so any suggestions would be appreciated :)

Edited by by0logic, 21 November 2010 - 12:55 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I had not tested your code, but this should correct it:
for($i=0;$i<=2;$i++){
    $filename = "accounts/" . $_SESSION['login'] . "/character" . $i . ".txt";
    if(file_exists($filename)){
        $file = fopen($filename, 'r');
        ${'characterFile' . $i} = fread($file, filesize($filename));
        ${'characterLines' . $i} = explode("\n", ${'characterFile' . $i});
        fclose($file);
    }
}
I would really not work with dynamic variables though, they are hard to keep track of.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
I agree. Why not just use multi-dimensional arrays?

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#4
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
Thanks for the fast replies. I had tested my initial code and what I had worked correctly. Now, I tried Nullw0rm's answer and I get a syntax error on the dynamic variable declaration (rel. line 7);

<?

	for($i=0;$i<=2;$i++){

		$filename = "character".$i.".txt";

		if(file_exists($filename)){

			$file = fopen($filename, 'r');

			$charFile = fread($file, filesize($filename));

			$("charSheet".$i) = explode("\n", $charFile);

			fclose($file);

		}

	}

    echo($charSheet0[0].$charSheet0[1].$charSheet0[2]."\n");

    echo($charSheet1[0].$charSheet1[1].charSheet1[2]."\n");

    echo($charSheet2[0].$charSheet2[1].$charSheet2[2]);

?>

I kinda thought it was the easiest way to handle multiple preformatted files but I believe you guys have much more experience so if you believe multi-dimensionnal arrays are the way to go, I'll look into that but I've never used one before so I'm not sure where to start...

well its 5:30 in the morning, I'll start by a good night of sleep ;)

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You used the wrong type of brackets in your dynamic variable code..
You can always use file() to get lines, you are wishing for an array of lines to be appended to an array?
$files = array();
for($i=0;$i<=2;$i++){
    $filename = "accounts/" . $_SESSION['login'] . "/character" . $i . ".txt";
    if(file_exists($filename)){
     //array[file] = lines
        $files[$i] = file($filename);
    }
}

echo $files[0][4]; //file 1, line 5
echo $files[1][0]; //file 2, line 1
print "<pre><br/>"; print_r($files); //print whole array
It is very late for me too, I apologize if I got your problem wrong, heh.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
Don't worry: you pinpointed the solution. I've got exactly what I need, thanks a lot! :thumbup: