php将字符串附加到stdClass对象

发布于 2024-12-18 07:11:02 字数 492 浏览 12 评论 0原文

我正在从数据库中提取记录,并且有一个名为 content_fr 的文件 _fr 是根据站点语言动态创建的。

从表中获取所有记录当然会给我 content_fr 字段。我需要做的是手动分配此 case _fr 中的后缀,如果我尝试连接 $row->content.'_fr' 我收到错误 注意:未定义的属性:stdClass::$content,这是有道理的,因为 $content 不存在,但 $content_fr 存在。使用标准数组,我可以像 $row['content_fr'] 那样执行此操作,并且工作正常,但现在使用 stdClass 我收到错误。

如何将 $row->content.'_fr' 转换为一个字符串,以便 php 将其视为 $row->content_fr

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-12-25 07:11:02

试试这样:

$row -> {'content_' . $language}; // assuming $language = 'fr'

Try it like this :

$row -> {'content_' . $language}; // assuming $language = 'fr'
寂寞美少年 2024-12-25 07:11:02

您可以按如下方式进行操作:

$row->{'content_fr'};

编辑:顺便说一句,这个问题已在这里被问过很多次。请参阅以前的线程,例如这个: 访问对象的属性,例如如果对象是数组

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.

请远离我 2024-12-25 07:11:02

尝试这样的事情:

$lang = 'fr';
$property = 'content_' . $lang; // content_fr
$row->$property = 'something';

Try something like this:

$lang = 'fr';
$property = 'content_' . $lang; // content_fr
$row->$property = 'something';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文