0

How I can display elements of the following object ? e.g : data -> percent_off

As I stored following like :

$input = file_get_contents("php://input");
$retun_val = json_decode($input);

stdClass Object
        (
        [id] => evt_151H4qEfYJJpTpgqdg5eQvIK
        [created] => 1416575036
        [livemode] =>
        [type] => coupon.created
        [data] => stdClass Object
        (
            [object] => stdClass Object
            (
                [id] => 5D
                [created] => 1416575036
                [percent_off] => 5
                [amount_off] =>
                [currency] =>
                [object] => coupon
                [livemode] =>
                [duration] => once
                [redeem_by] =>
                [max_redemptions] =>
                [times_redeemed] => 0
                [duration_in_months] =>
                [valid] => 1
                [metadata] => stdClass Object(  )
            )
        )
        [object] => event
        [pending_webhooks] => 1
        [request] => iar_5BeN55JpEDjH33
        [api_version] => 2014-10-07
        )

Edited by tNicknames

2
Contributors
1
Reply
15
Views
3 Years
Discussion Span
Last Post by broj1
0

Not sure if I understood the question but this recursive function will traverse the object, search for a property and display it:

function displayElement($obj, $el) {
    foreach($obj as $key => $val) {
        if(is_object($val)) {
            displayElement($val, $el);
        } else {
            if($key == $el) {
                echo "Found $key: $val<br>";
                die();
            } else {
                echo "Searching $key<br>";
            }
        } 
    }
}

So if you have an object like this:

$object1->id = 'evt_151H4qEfYJJpTpgqdg5eQvIK';
$object1->created = 1416575036;
$object1->livemode = '';
$object1->type = 'coupon.created';
$object1->data->id = '5D';
$object1->data->created = 1416575036;
$object1->data->percent_off = 5;
$object1->data->amount_off = '5D';
$object1->data->currency = '';
$object1->data->object = 'coupon';
$object1->data->duration = 'once';
$object1->data->redeem_by = '';
$object1->data->max_redemptions = '';
...

use the function this way:

displayElement($object1, 'percent_off');

It displays the first element found (might be not good for you since you have some repeating properties down the tree)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.