stdClass Object - split into table

569 Views Asked by At

I am using the GoCardless API to create a subscription. I have created it and now trying to show the payments in a table.

The API returns a stdClass Object with the upcoming payments in an array. How can I show these in a table?

 $SUBS=$client->subscriptions()->get("$SUB");


 Subscription Class
stdClass Object
(
    [id] => SB00024BNFEEHX
    [created_at] => 2020-03-07T20:20:05.025Z
    [amount] => 5998
    [currency] => GBP
    [status] => active
    [name] => 
    [start_date] => 2020-03-12
    [end_date] => 
    [interval] => 1
    [interval_unit] => monthly
    [day_of_month] => 
    [month] => 
    [count] => 
    [metadata] => stdClass Object
        (
            [subscription_number] => 
        )

    [payment_reference] => 
    [upcoming_payments] => Array
        (
            [0] => stdClass Object
                (
                    [charge_date] => 2020-03-12
                    [amount] => 5998
                )

            [1] => stdClass Object
                (
                    [charge_date] => 2020-04-14
                    [amount] => 5998
                )

            [2] => stdClass Object
                (
                    [charge_date] => 2020-05-12
                    [amount] => 5998
                )

            [3] => stdClass Object
                (
                    [charge_date] => 2020-06-12
                    [amount] => 5998
                )

From other questions I have tried

$array = json_decode(json_encode($SUBS),true);
echo"$array";

However this just displays/prints Array

1

There are 1 best solutions below

3
Ronak Dhoot On BEST ANSWER

You want to convert it an object as array so simple typecast will work

$SUBS=$client->subscriptions()->get("$SUB");
$SUBS = (array) $SUBS->upcoming_payments;

//for table
print '<table>';
foreach($SUBS as $SUB) {
   print '<tr><td>'.$SUB['charge_date'].'</td><td>'.$SUB['amount'].'</td></tr>';
}
print '<table>';