How to program a variable variable???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tienus
Forum Newbie
Posts: 3
Joined: Tue Jul 29, 2003 10:24 am

How to program a variable variable???

Post by Tienus »

I know that the following script doesn't work. But how do I get it to return "1234" on the last line? The last line should be interpreted as $AnyArray["justARandomKey"], but how :?:

// Init var $AnyArray as an array
$AnyArray = Array();

// Init vars for referencing
$nameArray = "AnyArray";
$nameKey = "justARandomKey";
$value = "1234";

// set the original array
$AnyArray[$nameKey] = $value;

// get the array by referencing values
echo $nameArray[$nameKey];
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: How to program a variable variable???

Post by nielsene »

Tienus wrote:I know that the following script doesn't work. But how do I get it to return "1234" on the last line? The last line should be interpreted as $AnyArray["justARandomKey"], but how :?:

// Init var $AnyArray as an array
$AnyArray = Array();

// Init vars for referencing
$nameArray = "AnyArray";
$nameKey = "justARandomKey";
$value = "1234";

// set the original array
$AnyArray[$nameKey] = $value;

// get the array by referencing values
echo $nameArray[$nameKey];
echo $$nameArray[$nameKey];
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Put single quotes round the variable $nameKey:

Code: Select all

<?php
$AnyArray['$nameKey'] = $value; 
echo $nameArray['$nameKey'];

?>
At a guess.
Tienus
Forum Newbie
Posts: 3
Joined: Tue Jul 29, 2003 10:24 am

Doesn't work for me

Post by Tienus »

Sorry Eric, but I tried that allready and it didn't work.

What am I doing wrong :?: :?: :?:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Doesn't work for me

Post by nielsene »

Tienus wrote:Sorry Eric, but I tried that allready and it didn't work.

What am I doing wrong :?: :?: :?:

Code: Select all

<?php
error_reporting(E_ALL);

$AnyArray=array();
$nameArray = "AnyArray";
$nameKey="aKey";
$value=1234;
$AnyArray[$nameKey]=$value;
echo ${$nameArray}[$nameKey];

?>
Normally $$varname works for variable variables. However the array index operation hapens before the variable name resolution, so you have to use braces to force the variable variable resolution before the indexing.
Didn't test before teh earlier post. This one works.
Tienus
Forum Newbie
Posts: 3
Joined: Tue Jul 29, 2003 10:24 am

Works like a charm

Post by Tienus »

I tried doing some things with the braces and the extra $ but never had the right combination or order with them :wink:

Thanks for helping me out on this one :D
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Ah - I see whats going on - you wanted indirection for arrays.
I didn't understand the question at first, thats why my previous posts are less than helpful - sorry.
Post Reply