Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 10, 2016, 12:40:42 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: [PHP] 'Dynamic variable name' ?  (Read 2985 times)

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
[PHP] 'Dynamic variable name' ?
« on: February 05, 2011, 03:53:27 PM »
This is something I've managed to do so far:
Code: PHP [Select]
  1. function generateMap {
  2.         $i = 0;
  3.                 $openMap = fopen("map/map_1-1.txt", "r");
  4.                         while (!feof($openMap)) {
  5.                                 $line = fgets($openMap, 1024);
  6.                                         $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, 04:22:33 PM by RedPillow »

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: [PHP] 'Dynamin variable name' ?
« Reply #1 on: February 05, 2011, 04:07:21 PM »
Why not use arrays instead?
- carpe noctem

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: [PHP] 'Dynamin variable name' ?
« Reply #2 on: February 05, 2011, 04:10:09 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: 2,790
    • View Profile
    • Donate to Member
Re: [PHP] 'Dynamin variable name' ?
« Reply #3 on: February 05, 2011, 04:16:11 PM »
Do as the man says, use an array.

RedPillow

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

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: [PHP] 'Dynamic variable name' ?
« Reply #5 on: February 05, 2011, 04:30:45 PM »
Instead of your idea of "$map$i", you'd use multi-dimension arrays, or an array of arrays... like
Code: PHP [Select]
  1. $ubermap[i] = str_split($line);
- carpe noctem

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: [PHP] 'Dynamic variable name' ?
« Reply #6 on: February 05, 2011, 05:00:57 PM »
Thanks!
It's working now as so:
(Btw, any 'better' way to thank like reputation + etc. ?)
Code: PHP [Select]
  1. $i = 0;
  2.         $openMap = fopen("map/map_1-1.txt", "r");
  3.                 while (!feof($openMap)) {
  4.                         $line = fgets($openMap);
  5.                         $map[$i] = str_split($line);
  6.                         $i++;
  7.                 }
  8. fclose($openMap);
  9.  
  10. for ($row = 0; $row <= 13; $row++)
  11. {
  12.     for ($col = 0; $col <= 13; $col++)
  13.     {
  14.                 if ($map[$row][$col] == 'X') {
  15.                         echo "<img src = 'img/wall/X.jpg'/>";}
  16.                 elseif ($map[$row][$col] == 'G') {
  17.                         echo "<img src = 'img/terrain/G.jpg'/>";}
  18.     }
  19. }
« Last Edit: February 05, 2011, 05:05:24 PM by RedPillow »

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: [PHP] 'Dynamic variable name' ?
« Reply #7 on: February 18, 2011, 08:50:15 AM »
(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: 2,790
    • View Profile
    • Donate to Member
Re: [PHP] 'Dynamic variable name' ?
« Reply #8 on: February 18, 2011, 08:56:01 AM »
No reputation system, Eternal Gratefulness will also work nicely.
Or a (small) donation to the person that helped you out so nicely :-[

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: [PHP] 'Dynamic variable name' ?
« Reply #9 on: February 18, 2011, 09:00:52 AM »
Ath: obviously, I'm embarrassed I didn't think of that first!  :-[