简体   繁体   中英

How can I access a property with an invalid name?

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]-> = $firstname;

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

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

$insertArray[0]-> = $firstname;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM