jimmyoneshot Posted September 6, 2011 Share Posted September 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/ Share on other sites More sharing options...
Pikachu2000 Posted September 6, 2011 Share Posted September 6, 2011 This should work for you. $row_item['comment' . $i . '_name'] = "john"; Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1266247 Share on other sites More sharing options...
jimmyoneshot Posted September 8, 2011 Author Share Posted September 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267094 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 <?php $num = 1; $var1 = "blah"; $var2 = "hello"; echo ${'var'.$num} ?> Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267098 Share on other sites More sharing options...
jimmyoneshot Posted September 8, 2011 Author Share Posted September 8, 2011 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'] ? Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267139 Share on other sites More sharing options...
jimmyoneshot Posted September 8, 2011 Author Share Posted September 8, 2011 Nevermind. I just tested it and it works perfect. Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267146 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267151 Share on other sites More sharing options...
salathe Posted September 8, 2011 Share Posted September 8, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267173 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 I agree completely with the above poster. Though it is essentially the same, you avoid possible naming collisions and debugging becomes much easier when you can print_r() and verify how your data is structured. Quote Link to comment https://forums.phpfreaks.com/topic/246581-concatenation-inside-array-element-names/#findComment-1267204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.