'How to access a var with "-" in PHP [duplicate]
For first time I found this problem, see I've a array of objetcs and I access them as follow:
$arrayOfObjects[$pos]->value;
But my problem is that one of the array values have a "-" meaning the value is e-mail
so when I do this:
$arrayOfObjects[$pos]->e-mail;
I get an error, how do I handle this? The var can't be changed!!
Solution 1:[1]
If you absolutely have to have the hyphens, you can access it like:
$arrayOfObjects[$pos]->{'e-mail'};
Solution 2:[2]
You cannot access a variable with a dash in it in that fashion.
Try this:
$arrayOfObjects[$pos]['e-mail'];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kalley |
Solution 2 | Naftali |