Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php appending string to stdClass object

I'm pulling records from database, and I have a filed called content_fr the _fr is being dynamicaly created based on a site language.

Getting all records from the table gives me of course content_fr field. What I need to do is manually assign the suffix in this case _fr if I try to concatenate $row->content.'_fr' I get the error Notice: Undefined property: stdClass::$content, which makes sense since $content does not exist, but $content_fr does. Using standard arrays I was able to do it like $row['content_fr'], and worked fine, but now using stdClass I'm getting an error.

How can I convert $row->content.'_fr' into one string, so that php sees it as $row->content_fr ?

like image 571
Mike Avatar asked Dec 04 '22 06:12

Mike


2 Answers

Try it like this :

$row -> {'content_' . $language}; // assuming $language = 'fr'
like image 91
Tom van der Woerdt Avatar answered Dec 19 '22 19:12

Tom van der Woerdt


You can do it as follows:

$row->{'content_fr'};

EDIT: By the way, this question has been asked here many times. See previous threads such as this one: Accessing object's properties like if the objects were arrays.

like image 38
Godwin Avatar answered Dec 19 '22 21:12

Godwin