如何访问名称无效的属性? [英] How can I access a property with an invalid name?

查看:181
本文介绍了如何访问名称无效的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要向将要通过第三方API发送的stdClass对象添加数据,因此我给这个对象的元素的名称实际上是由该外部服务定义的。

I'm adding data to a stdClass object that is going to be sent through a 3rd party API and so the names I am giving to the elements of this object are actually being defined by that external service.

$insertArray = array();
$insertArray[0] = new stdclass();
$insertArray[0]->Name = $name;
$insertArray[0]->PhoneNumber = $phone;

这一切都在奇妙地工作,直到我碰到一个属性无效的名称

This was all working wonderfully until I came across a property with an invalid name:

$insertArray[0]->First.Name = $firstname;

所以这不是有效的PHP语法,

So that isn't valid PHP syntax, so is there a way around this?

推荐答案

感谢评论(@AbraCadaver),复杂(curly)语法是去无效变量或属性名称的方法:

Thanks to the comments (@AbraCadaver), Complex (curly) syntax is the way to go for invalid variables or property names:

$insertArray[0]->{"First.Name"} = $firstname;

这篇关于如何访问名称无效的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆