Jump to content

Concatenation inside Array Element Names


jimmyoneshot

Recommended Posts

I'm wondering if this is at all possible:-

 


$i = 7;

$row_item['comment$i_name'] = "john";

 

with the $i in $row_item['item$i_id'] being replaced with whatever $i is set as sort of like this:-

 

$row_item['comment.'$i'._name']

 

Is this sort of concatenation possible inside array element names as I can't seem to get it to work?

Thanks very much Pikachu that worked great.

 

While we are on the subject do you happen to know if there is any way possible to do this in variable NAMES? e.g. :-

 


$num = 1;

$var1 = "blah";

$var2 = "hello";


echo $var<$num>

 

With <$num> being replaced with whatever is in $num? I very much doubt this is possible but was just wondering.

Thanks xyph.

 

That should be a great help in future. I'm gonna start using it straight away.

 

So let's say I have this:-

 


$row_items['heading'] = "blah";

$row_comments['heading'] = "hello";

$type = "comments";

echo ${'row_'.$type}['heading']

 

Would that work assuming I want to echo whatever is in $row_comments['heading'] ?

The best way to learn!

 

Some good reading: http://php.net/manual/en/language.variables.variable.php

 

Any when using the manual, always remember that there is always hidden gold in the user comments. There is a little trash to, so be sure to read thoroughly

Variable variables have their uses, but in many cases a basic array would do the job better.  To take the example from above, it would (arguably) be better to have:

 

$row['items']['heading'] = "blah";
$row['comments']['heading'] = "hello";
$type = "comments";
echo $row[$type]['heading'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.