MartynA Posted July 25, 2006 Share Posted July 25, 2006 Hi,I hope one of you guys can help, I have a slight problem involving arrays and variables:I have an object array, $row2 populated with data pulled from mysql. Hence $row2->name grabs the name part of the query. Now, i want to do a foreach loop to display some of these, specifically user2, user7 etc.Long story short, how do I concatenate $row2->user with the array key - or any thing else for that matter:[code]$variable = "7";print "$row2->user" . "$variable";[/code]This doesn't work, and only outputs 7, not the contents of $row2->user7.I suspect I'm missing something obvious here, anyone wanna fill me in?Thanks,Mart Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/ Share on other sites More sharing options...
obsidian Posted July 25, 2006 Share Posted July 25, 2006 one possible solution:[code]<?php$variable = 7;eval("print \"\$row2->user$variable;");?>[/code]hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63610 Share on other sites More sharing options...
MartynA Posted July 25, 2006 Author Share Posted July 25, 2006 Thanks; close but no cigar.Your code gave me an error so I looked up the eval syntax and came up with [code]eval("print \"\$row2->user$variable\";");[/code]However, that doesn't output anything. At all. What I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63622 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 try this??[code]print ${'row2->user'.$variable};[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63624 Share on other sites More sharing options...
kenrbnsn Posted July 25, 2006 Share Posted July 25, 2006 Try changing your mysql_fetch to mysql_fetch_assoc(), then you would reference the field with $row2['user']and the code to print:[code]<?php print $row2['user'] . $variable; ?>[/code]The next question is ... why do you want to do this? Maybe if you explain what you want to do, we can come up with an easier way of doing it.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63626 Share on other sites More sharing options...
MartynA Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=kenrbnsn link=topic=101836.msg403418#msg403418 date=1153860104]Try changing your mysql_fetch to mysql_fetch_assoc(), then you would reference the field with $row2['user']and the code to print:[code]<?php print $row2['user'] . $variable; ?>[/code]The next question is ... why do you want to do this? Maybe if you explain what you want to do, we can come up with an easier way of doing it.Ken[/quote]Thanks Ken, that did it. For any future reference, I was wanting to foreach through an array containing values such as 5,7,6 and then echo out the equivalent, e.g. $row2->user$valueinarray. That way I could define what information I wanted printing just by passing an array to the function. Thanks, and sorry for not explaining myself so well. Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63647 Share on other sites More sharing options...
ShogunWarrior Posted July 25, 2006 Share Posted July 25, 2006 This will work:print( $row2->{'user' .$variable} ); Quote Link to comment https://forums.phpfreaks.com/topic/15621-concatenation-of-two-variables-sort-of/#findComment-63722 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.