Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to access object properties with hyphen? [duplicate]

Tags:

json

php

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?

like image 605
Hendry H. Avatar asked Sep 07 '25 22:09

Hendry H.


1 Answers

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!