4

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
  • 37,241
  • 25
  • 195
  • 267
Hendry H.
  • 1,482
  • 3
  • 13
  • 27

3 Answers3

8

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
5

Use the {} syntax:

echo $response->messages[0]->{'error-text'};
MrCode
  • 63,975
  • 10
  • 90
  • 112
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