1

I have my code in PHP which is returning this Array of data:

GoCardlessPro\Core\ListResponse Object
(
    [records] => Array
        (
            [0] => GoCardlessPro\Resources\Mandate Object
                (
                    [model_name:protected] => Mandate
                    [created_at:protected] => 2017-04-01T16:49:09.642Z
                    [id:protected] => ID001
                    [links:protected] => stdClass Object
                        (
                            [customer_bank_account] => CB001
                            [creditor] => CR001
                            [customer] => CU001
                        )

                    [metadata:protected] => stdClass Object
                        (
                        )

                    [next_possible_charge_date:protected] => 2017-04-06
                    [payments_require_approval:protected] => 
                    [reference:protected] => RE001
                    [scheme:protected] => bacs
                    [status:protected] => active
                    [data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
                        (
                            [id] => 123
                            [created_at] => 2017-04-01T16:49:09.642Z
                            [reference] => RE001
                            [status] => active
                            [scheme] => bacs
                            [next_possible_charge_date] => 2017-04-06
                            [payments_require_approval] => 
                            [metadata] => stdClass Object
                                (
                                )

                            [links] => stdClass Object
                                (
                                    [customer_bank_account] => 001
                                    [creditor] => CR001
                                    [customer] => CU001
                                )

                        )

                    [api_response] => 
                )

        )
)

I want to be able to read the ID of the first item in therecords array.

This data is contained inside a variable called $GC_Mandate;

I have tried these:

echo $GC_Mandate->records->{0}->id;

echo $GC_Mandate->records->0->id;

echo $GC_Mandate->records->[0]->id;

$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;

But none will return the data

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
charlie
  • 415
  • 4
  • 35
  • 83

4 Answers4

1

To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].

However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.

My guess would be getId(), so the full syntax would become

$GC_Mandate->records[ 0 ]->getId()

But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.


Turns out (provided I'm linking to the correct github repository) you can do:

$GC_Mandate->records[ 0 ]->id

since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.


1. visibility in PHP
2. magic methods in PHP

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • just tried your second option but thats not showing the value – charlie Apr 01 '17 at 20:58
  • @charlie Then I'm afraid you might have to look a little further into their documentation. Meanwhile, I've also tagged your question with [tag:gocardless]. Perhaps that garners the appropriate attention to you question. – Decent Dabbler Apr 01 '17 at 21:10
  • @charlie Also, have you enabled the [reporting of errors](https://secure.php.net/manual/en/function.error-reporting.php) on your development server? Look into the errors log (or display them directly with `ini_set('display_errors', '1');`, while on the development server (don't enable displaying of errors on a live server though)). That should give you more clues about what's going on. – Decent Dabbler Apr 01 '17 at 21:24
  • i can loop through `$GC_Mandate->records` and echo the `ID` - not sure if that helps with trying to get the first item directly or should i just loop once and then break the loop? – charlie Apr 01 '17 at 21:38
  • @charlie If you can loop and access the `id` (or is it `ID`? Case is important here, as variables are case-sensitive), then `$GC_Mandate->records[ 0 ]->id` (or `->ID`) should also work, provided all the information (the array dump, array indexes, case-sensitivity, etc.) you posted so far is accurate. Please update your question with the exact (case-sensitive, etc) relevant code of your loop as well. – Decent Dabbler Apr 01 '17 at 22:01
0

I can't comment so I guess I'll have to post.

You should try to print_r($GC_Mandate); and see what it gives out and then go from there.

Marc Alexander
  • 761
  • 11
  • 24
0

Try $GC_Mandate->records[0]->__get('id')

gmc
  • 3,910
  • 2
  • 31
  • 44
0

it will return all data ..for perticulat data put this in foreach loop

 print_r($GC_Mandate['records']);
Manthan Patel
  • 1,784
  • 19
  • 23