3

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?

Rizier123
  • 57,440
  • 16
  • 89
  • 140
AdamJones
  • 653
  • 1
  • 13
  • 35

1 Answers1

5

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

$insertArray[0]->{"First.Name"} = $firstname;
Rizier123
  • 57,440
  • 16
  • 89
  • 140
AdamJones
  • 653
  • 1
  • 13
  • 35