DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: [PHP] 'Dynamic variable name' ?  (Read 9,565 times)

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
[PHP] 'Dynamic variable name' ?
« on: February 05, 2011, 09:53 PM »
This is something I've managed to do so far:
Code: (php)

function generateMap {
	$i = 0;
		$openMap = fopen("map/map_1-1.txt", "r");
			while (!feof($openMap)) {
				$line = fgets($openMap, 1024);
					$map = str_split($line);

The problem is, that I want to name the array $map like $map$i  or $map + $i...
So that it's like $map1 = blah, $map2 = blah, $map3 = blah ...

Do you get me?
How do I do that?
« Last Edit: February 05, 2011, 10:22 PM by RedPillow »

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: [PHP] 'Dynamin variable name' ?
« Reply #1 on: February 05, 2011, 10:07 PM »
Why not use arrays instead?
- carpe noctem

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: [PHP] 'Dynamin variable name' ?
« Reply #2 on: February 05, 2011, 10:10 PM »
Whaat?

I want to get every line of the text-file.
Then spilt those lines to arrays called map1, map2, map3 ... where each character of the line is one cell of an array.
But how do I name the $map differently after every loop?

like $map$i or somrthing?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: [PHP] 'Dynamin variable name' ?
« Reply #3 on: February 05, 2011, 10:16 PM »
Do as the man says, use an array.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: [PHP] 'Dynamin variable name' ?
« Reply #4 on: February 05, 2011, 10:17 PM »
What do you mean?
Aren't I using arrays when splitting the lines to an array?

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: [PHP] 'Dynamic variable name' ?
« Reply #5 on: February 05, 2011, 10:30 PM »
Instead of your idea of "$map$i", you'd use multi-dimension arrays, or an array of arrays... like
Code: (php)
$ubermap[i] = str_split($line);
- carpe noctem

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: [PHP] 'Dynamic variable name' ?
« Reply #6 on: February 05, 2011, 11:00 PM »
Thanks!
It's working now as so:
(Btw, any 'better' way to thank like reputation + etc. ?)
Code: (php)
	$i = 0;
	$openMap = fopen("map/map_1-1.txt", "r");
		while (!feof($openMap)) {
			$line = fgets($openMap);
			$map[$i] = str_split($line);
			$i++;
		}
fclose($openMap);

for ($row = 0; $row <= 13; $row++)
{
    for ($col = 0; $col <= 13; $col++)
    {
		if ($map[$row][$col] == 'X') {
			echo "<img src = 'img/wall/X.jpg'/>";}
		elseif ($map[$row][$col] == 'G') {
			echo "<img src = 'img/terrain/G.jpg'/>";}
    }
}
« Last Edit: February 05, 2011, 11:05 PM by RedPillow »

jgpaiva

  • Artificial Idiocy
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • Cody Award
    • Participated in NANY Challenge Once
    • Coding Snacks
    • Portugal Inhabitant
    • Linux User
Re: [PHP] 'Dynamic variable name' ?
« Reply #7 on: February 18, 2011, 02:50 PM »
(Btw, any 'better' way to thank like reputation + etc. ?)
No reputation system, Eternal Gratefulness will also work nicely. But if there was a reputation system in place, f0dder would obviously be off the scale anyways :P

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: [PHP] 'Dynamic variable name' ?
« Reply #8 on: February 18, 2011, 02:56 PM »
No reputation system, Eternal Gratefulness will also work nicely.
Or a (small) donation to the person that helped you out so nicely :-[
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

jgpaiva

  • Artificial Idiocy
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • Cody Award
    • Participated in NANY Challenge Once
    • Coding Snacks
    • Portugal Inhabitant
    • Linux User
Re: [PHP] 'Dynamic variable name' ?
« Reply #9 on: February 18, 2011, 03:00 PM »
Ath: obviously, I'm embarrassed I didn't think of that first!  :-[

Original address: https://www.donationcoder.com/forum/index.php?topic=25532.0