I\'m new to PHP and JSON, so hoping someone can help me.
I have a PHP which a 3rd party performs a POST against, delivering JSON data. An example of the data sent wo
you have multi levels of objects so use:
echo($event_json->data->object->fee);
Basically you are echoing fee
, which is a member of the object name object
, which in turn is the attribute of an object named data
, which again in turn is a member of your $event_json
object.
When you do a json_decode, you get an array of this data. So, you can access data like this:
$event_json['data']['object']['fee']
good luck
First, I would suggest passing true
as the second argument to json_decode
. This results in an associative array rather than an object, which makes it a little easier to work with.
Then, you can do this:
$fee = $event_json['data']['object']['fee'];
You can chain these square brackets as many times as needed to delve deeper into the array.
You can quickly acheive this in following manner:
$body = @file_get_contents('php://input');
$event_json = json_decode($body, true);
print $event_json['data']['object']['fee'];