How can I access a property with an invalid name?

纵然是瞬间 提交于 2019-11-26 02:14:55

问题


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;

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


回答1:


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

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


来源:https://stackoverflow.com/questions/33157296/how-can-i-access-a-property-with-an-invalid-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!