Php variables/arrays

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
Is it possible to name an array based on two other variable names?

E.g.

$str1 . $str2 = = array();

So if strr1 = choc, str=cy, the array would be called "choccy"?
 
Nope, can't do that, unless there's some way to parse a string as code that I don't know of... but I doubt it. I'll feel silly if someone says you can, now... lol.

Why do you want to do that, anyway? (might be an alternative solution :))
 
furnace said:
Why do you want to do that, anyway? (might be an alternative solution :))
As this isn't working:

Code:
$count = 0;
foreach ($arr_im_iname as &$id){
//blah blah blah
	foreach ($arr_im_iseq[$count] as &$id){
	echo $arr_im_iseq[$count][0];
	}
$count = $count +1;
}

Off out, will explain more late unless my flaw is obvious.
 
What context is this in? It shouldn't really be needed; if you are uncertain about the contents of $str1/2 it could prove hazardous and you may lose your bearings, and on the other hand if their contents are known or hard-coded in, you should know their names anyway. :P
 
Code:
$count = 0;
foreach ($arr_im_iname as &$id){
//blah blah blah
	foreach ($arr_im_iseq[$count] as &$id){
	echo $arr_im_iseq[$count][0];
	}
$count = $count +1;
}
Hrm, still a bit confused so might have to wait until you're back..!

This one will;
- Set i to 0
- For each item in $arr_im_name:
--- For each item in the array $arr_im_iseq[$i], put the items key (e.g. the "0" from "arr_im_iseq[$i][0]") in $key, and its value (eg the "7" in "arr_im_iseq[$i]=7") in $value
------ Echo the key and the value of this item
--- Add 1 to i

Code:
$i=0;

foreach ($arr_im_iname as $id){

	foreach($arr_im_iseq[$i] as $key=>$value){

		echo $key.": ".$value."<br>";

	}

	$i++;

}

Don't know if that helps! Will try later when you've explained more :)
 
Inquisitor said:
Why exactly would you want to dynamically name a variable?

I do this :-)

Backend CMS system I am creating using the __construct function in a class and I only define one of the many classes.

$com_name = 'com_' . $this->comName;
${$com_name} = new $com_name;

It can come in use sometimes and is worth knowing :)
 
Jaffa_Cake said:
I do this :-)

Backend CMS system I am creating using the __construct function in a class and I only define one of the many classes.

$com_name = 'com_' . $this->comName;
${$com_name} = new $com_name;

It can come in use sometimes and is worth knowing :)
Que?
 
Polymorphism can make good use of variable variables. I prefer to keep it simple and just use a switch or similar. Readability is far more important to me.
 
Back
Top Bottom