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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'] ?

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.