variables and arrays

this is annoying and confusing...

Page 1 of 1

5 Replies - 1645 Views - Last Post: 16 May 2005 - 07:10 PM Rate Topic: -----

#1 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

variables and arrays

Posted 16 May 2005 - 05:13 PM

OK, so I know what I want to do, and how it ought to work, but it doesn't...

basically, I'm working on some neat navigation system, but I need to put some values into an array dynamically for it, allow me to demonstrate:

this code works:
$ps = "p";
$$ps = "boo!";
echo $p;


producing the expected result, boo!

however this doesn't:
$ps = "p[0]";
$$ps = "boo!";
echo $p[0];



it gives nothing, $p[0] isn't set, I know it's because I'm using []'s and php isn't doing what I want it to, but I can't for the life of me think of a workaround for this.

Is This A Good Question/Topic? 0
  • +

Replies To: variables and arrays

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: variables and arrays

Posted 16 May 2005 - 05:24 PM

I think you may need to use the eval() function, when I'm dealing with dynamic variable names in ColdFusion or PHP, I almost always have to eval the dynamic variable name to accomplish what I wanna do:

http://us3.php.net/m...nction.eval.php

Basically, you will evaluate something like eval('$'.$variable); essentially creating a new variable name, then evaluating it
Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: variables and arrays

Posted 16 May 2005 - 05:28 PM

Also, you may note someone who had a similar problem, he used the eval("$variable$a") method... haven't tried it myself, here was his post:

Quote

I couldn't find an example of what I wanted to do, which was dynamically create and handle enumerated variables from a form, so here it is in simplified form:

<?php
echo("<table>");
$counter=0;
for ($a=0;$a<count($timeslots);$a++) {
  echo("<tr>");
  for ($i=0;$i<3;$i++) {
                    $counter++;
                    $fieldwithcounter="fieldname$counter";
                    $var1=$_POST[$fieldwithcounter];
                    $nextfield="second$counter";
                    $var2=$_POST[$nextfield];
                    $thirdfield="lastfield$counter";
                    $var3=$_POST[$thirdfield];
eval("\$grouprow$a.=\"$var1^^$var2^^$var3^^$var4\";");
// creates $grouprow0, $grouprow1, $grouprow2, ...

echo("<td>
    First field
        <input type='text' name='fieldname$counter' value='$var1'><br>

    Second field
      <input type='text' name='second$counter' value='$var2'><br>

    Last field
      <input type='text' name='lastfield$counter' value='$var3'>
      </td>");
  }
echo("</tr>");
}
echo("</table>");
?>

Was This Post Helpful? 0
  • +
  • -

#4 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: variables and arrays

Posted 16 May 2005 - 06:18 PM

eval() didn't help much, however after staring at my monitor blankly for a while I had a flash of inspiriation and got this to work thusly:

$a = 0;
$p{$a} = "something";



this correctly sets $p[0] to "something" and was far simpler than what I was trying to do... :)

This post has been edited by Spider: 16 May 2005 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: variables and arrays

Posted 16 May 2005 - 06:24 PM

Doh. Good call!
Was This Post Helpful? 0
  • +
  • -

#6 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: variables and arrays

Posted 16 May 2005 - 07:10 PM

Holy crap! I remember wanting to do something like that once Spider. I can't quite remember what I wanted to do, but I ended up using the eval().


I'll have a look into my code and see if I can find it.




Oh and thanks for this excellent find in PHP code parsing! I will certainly find ways to use it! :D

This post has been edited by hotsnoj: 16 May 2005 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1