I am fetching data from JSON in the form of PHP array. But I am not able to fetch it due to strange OBJECT titles.
E.g. here is simply example
[car] => stdClass Object
(
[model] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => Good
)
)
Now I can fetch "condition" like this $car->model->condition;
But in my case, the JSON is like this
[car] => stdClass Object
(
[field_set_key="profile",username="sammy"] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => Good
)
)
I am not able to fetch "condition" value due to this strange object [field_set_key="profile",username="sammy"]
What should I do?
$car->[ ?????? ]->condition;
Using the {}
syntax you can address that like this
echo $car->{'field_set_key="profile",username="sammy"'}->condition;
Its ugly and it would really be better if you got the people producing this data to fix their code.
Example
$car = new stdClass;
$b = new stdClass;
$b->year = 2018;
$b->company = 'Honda';
$b->condition = 'good';
$car->{'field_set_key="profile",username="sammy"'} = $b;
print_r($car) . PHP_EOL;
echo 'The condition of the '
. $car->{'field_set_key="profile",username="sammy"'}->year . ' '
. $car->{'field_set_key="profile",username="sammy"'}->company
. ' is ' . $car->{'field_set_key="profile",username="sammy"'}->condition;
RESULTS
stdClass Object
(
[field_set_key="profile",username="sammy"] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => good
)
)
The condition of the 2018 Honda is good