2

The json format.

{
  "message-count":"1",
  "messages":[
    {
    "status":"returnCode",
    "error-text":"error-message"
    }
  ]
}

In php, I successfully get "status" value with $response->messages[0]->status
But when I wanted to access "error-text" properties, the code $response->messages[0]->error-text gives me error. How to access object properties with hyphen?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Hendry H.
  • 1,444
  • 2
  • 12
  • 27

3 Answers3

7

here is the way!

$object->{"message-count"};
$response->messages[0]->{'error-text'};

hope this helps


any string (bytes sequence) can be used as a class field

$object->{"123"} = 10; // numbers
$object->{"{a}"} = 10; // special characters
$object->{"òòèè"} = 10; // non ascii characters
3

Use the {} syntax:

echo $response->messages[0]->{'error-text'};
MrCode
  • 61,589
  • 10
  • 82
  • 110
0

Please, use standard PHP feature - accessing variables within curly braces:

class t {}
$a = new t();
$a->{"o-o"} = 1;
echo $a->{"o-o"};

So, you need to write $response->messages[0]->{"error-text"}.

Yorie
  • 151
  • 1
  • 4