Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,625 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!




variables and arrays

 
Reply to this topicStart new topic

variables and arrays, this is annoying and confusing...

Spider
post 16 May, 2005 - 04:13 PM
Post #1


Arachnid

****
Joined: 10 Jul, 2002
Posts: 769


My Contributions


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:
CODE
$ps = "p";
$$ps = "boo!";
echo $p;


producing the expected result, boo!

however this doesn't:
CODE
$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.
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 16 May, 2005 - 04:24 PM
Post #2


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


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/manual/en/function.eval.php

Basically, you will evaluate something like eval('$'.$variable); essentially creating a new variable name, then evaluating it
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 16 May, 2005 - 04:28 PM
Post #3


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


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>");
?>
User is offlineProfile CardPM

Go to the top of the page

Spider
post 16 May, 2005 - 05:18 PM
Post #4


Arachnid

****
Joined: 10 Jul, 2002
Posts: 769


My Contributions


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:

CODE

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


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

This post has been edited by Spider: 16 May, 2005 - 05:21 PM
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 16 May, 2005 - 05:24 PM
Post #5


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,846



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


Doh. Good call!
User is offlineProfile CardPM

Go to the top of the page

snoj
post 16 May, 2005 - 06:10 PM
Post #6


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


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! biggrin.gif

This post has been edited by hotsnoj: 16 May, 2005 - 06:12 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 03:30AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month