How to get variables inside array/objects

I have this output of $a

Array
(
    [id] => cus
    [object] => customer
    [account_balance] => 0
    [created] => 15217df
    [currency] => 
    [default_source] => card
    [delinquent] => 
    [description] => Customer forfdgfdg
    [discount] => 
    [email] => 
    [invoice_prefix] => 353593C
    [livemode] => 1
    [metadata] => Array
        (
        )

    [shipping] => 
    [sources] => stdClass Object
        (
            [object] => list
            [data] => Array
                (
                    [0] => stdClass Object
(
[id] => card_fortest1

Open in new window


I want to be able to get "sources" so I can get sources[0][id], sources[1][id]..etc

Is there an easier way to do so?
LVL 1
Jazzy 1012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
OK. You have a mix of array and objects. For arrays, you access members with teh square brackets []. For object members you access them with the -> indicator.

You would access the sources property like so:

$a['sources']

That is an object, so would then access the properties of that object like so:

$a['sources']->data.

data is an array of objects, so if you want to loop through them, you could do:

foreach ($a['sources']->data as $item):
    echo $item->id;
endforeach;
0
Jazzy 1012Author Commented:
when I do that that id keeps adding to each other.
For example the first loop will have one ID
the second loop will have the one its on & the previous one.
0
Chris StanyonWebDevCommented:
I'm guessing that it's just echoing out the IDs in one long row. When you say you want to 'get' the ID - what exactly do you mean?

Have a look at this:

foreach ($a['sources']->data as $item):
    printf("<p>ID : %s</p>", $item->id);
endforeach;

Open in new window

That will now echo out each ID on it's own line.
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Julian HansenCommented:
Can you show us your code and more of your data.
Also can you elaborate on what you are wanting to do. You say
I want to be able to get "sources" so I can get sources[0][id], sources[1][id]..etc
Are you saying you want to produce a new array with id's in them
OR
Do you just want to know how to access each of the id's in the sources property of the object?

Give us a use case so we can better understand the requirement.
0
Jazzy 1012Author Commented:
Here is the full code:
Array
(
    [id] => cus_C
    [object] => customer
    [account_balance] => 0
    [created] => 15
    [currency] => 
    [default_source] => card_1
    [delinquent] => 
    [description] => Customer for
    [discount] => 
    [email] => 
    [invoice_prefix] => 353593C
    [livemode] => 1
    [metadata] => Array
        (
        )

    [shipping] => 
    [sources] => stdClass Object
        (
            [object] => list
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => card_1
                            [object] => card
                            [address_city] => 
                            [address_country] => 
                            [address_line1] => 
                            [address_line1_check] => 
                            [address_line2] => 
                            [address_state] => 
                            [address_zip] => 
                            [address_zip_check] => 
                            [brand] => American Express
                            [country] => US
                            [customer] => cus_
                            [cvc_check] => pass
                            [dynamic_last4] => 
                            [exp_month] => 22
                            [exp_year] => 2892
                            [fingerprint] => n
                            [funding] => credit
                            [last4] => 1222
                            [metadata] => Array
                                (
                                )

                            [name] => 
                            [tokenization_method] => 
                        )

                    [1] => stdClass Object
                        (
                            [id] => card_1
                            [object] => card
                            [address_city] => 
                            [address_country] => 
                            [address_line1] => 
                            [address_line1_check] => 
                            [address_line2] => 
                            [address_state] => 
                            [address_zip] => 
                            [address_zip_check] => 
                            [brand] => Visa
                            [country] => 
                            [customer] => cus546456
                            [cvc_check] => pass
                            [dynamic_last4] => 
                            [exp_month] => 5
                            [exp_year] => 2070
                            [fingerprint] => 8Bvm
                            [funding] => credit
                            [last4] => 8781
                            [metadata] => Array
                                (
                                )

                            [name] => 
                            [tokenization_method] => 
                        )

                    [2] => stdClass Object
                        (
                            [id] => card_1546456
                            [object] => card
                            [address_city] => 
                            [address_country] => 
                            [address_line1] => 
                            [address_line1_check] => 
                            [address_line2] => 
                            [address_state] => 
                            [address_zip] => 
                            [address_zip_check] => 
                            [brand] => American Express
                            [country] => US
                            [customer] => cus_54645
                            [cvc_check] => pass
                            [dynamic_last4] => 
                            [exp_month] => 23
                            [exp_year] => 2078
                            [fingerprint] => Ldsddf
                            [funding] => credit
                            [last4] => 1232
                            [metadata] => Array
                                (
                                )

                            [name] => 
                            [tokenization_method] => 
                        )

                    [3] => stdClass Object
                        (
                            [id] => card_1Cfgfdgfdgfdgz
                            [object] => card
                            [address_city] => 
                            [address_country] => 
                            [address_line1] => 
                            [address_line1_check] => 
                            [address_line2] => 
                            [address_state] => 
                            [address_zip] => 
                            [address_zip_check] => 
                            [brand] => American Express
                            [country] => US
                            [customer] => cus_3434
                            [cvc_check] => pass
                            [dynamic_last4] => 
                            [exp_month] => 11
                            [exp_year] => 2078
                            [fingerprint] => 
                            [funding] => credit
                            [last4] => 1611
                            [metadata] => Array
                                (
                                )

                            [name] => test
                            [tokenization_method] => 
                        )

                )

            [has_more] => 
            [total_count] => 4
            [url] => /v1/custfgdfg
        )

    [subscriptions] => stdClass Object
        (
            [object] => list
            [data] => Array
                (
                )

            [has_more] => 
            [total_count] => 0
            [url] => /v1/cdsfdg
        )

    [tax_info] => 
    [tax_info_verification] => 
)

Open in new window


I have a variable called $lastfour & I want to to check if the last four in the sources equal to my last four, echo the ID for that card.
0
Chris StanyonWebDevCommented:
OK. To get the last four of the source data array, you would use this:

$dataLastFour = array_slice($a['sources']->data, -4, 4);

You don't say what your $lastfour variable contains, so I don't really understand the last part of your question.

If you just want to get the IDs from those last 4, then something like this:

$ids = array_map(function($x) { return $x->id; }, $dataLastFour);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
As a matter of interest on another question I proposed one of the array functions that takes a function as a parameter as an alternative to foreach - thinking that it would perform better - however on profiling it I found it ran significantly slower. In the case of array_map 3x slower than a foreach. I don't think it makes any difference in this case but if you are interested in performance then it is an interesting point.

On that note here is a solution that uses just an array to get the last 4 ID's in one loop
<?php
$data = json_decode(file_get_contents('data.json'));

$sources = $data->sources->data;
$len = count($sources);

for($i = max(0, $len - 4); $i < $len;$i++) {
	$ids[] = $sources[$i]->id;
}

echo "<pre>" . print_r($ids, true) . "</pre>";

Open in new window

I have put the data into a JSON file to make it easier to work with the above code - you would simply replace line 2 with whatever your data source is.

JSON code provided here
{data.json}
{
   "id":"cus_C",
   "object":"customer",
   "account_balance":"0",
   "created":"15",
   "currency":"",
   "default_source":"card_1",
   "delinquent":"",
   "description":"Customer for",
   "discount":"",
   "email":"",
   "invoice_prefix":"353593C",
   "livemode":"1",
   "metadata":[

   ],
   "shipping":"",
   "sources":{
      "object":"list",
      "data":[
         {
            "id":"card_1",
            "object":"card",
            "address_city":"",
            "address_country":"",
            "address_line1":"",
            "address_line1_check":"",
            "address_line2":"",
            "address_state":"",
            "address_zip":"",
            "address_zip_check":"",
            "brand":"American Express",
            "country":"US",
            "customer":"cus_",
            "cvc_check":"pass",
            "dynamic_last4":"",
            "exp_month":"22",
            "exp_year":"2892",
            "fingerprint":"n",
            "funding":"credit",
            "last4":"1222",
            "metadata":[

            ],
            "name":"",
            "tokenization_method":""
         },
         {
            "id":"card_1",
            "object":"card",
            "address_city":"",
            "address_country":"",
            "address_line1":"",
            "address_line1_check":"",
            "address_line2":"",
            "address_state":"",
            "address_zip":"",
            "address_zip_check":"",
            "brand":"Visa",
            "country":"",
            "customer":"cus546456",
            "cvc_check":"pass",
            "dynamic_last4":"",
            "exp_month":"5",
            "exp_year":"2070",
            "fingerprint":"8Bvm",
            "funding":"credit",
            "last4":"8781",
            "metadata":[

            ],
            "name":"",
            "tokenization_method":""
         },
         {
            "id":"card_1546456",
            "object":"card",
            "address_city":"",
            "address_country":"",
            "address_line1":"",
            "address_line1_check":"",
            "address_line2":"",
            "address_state":"",
            "address_zip":"",
            "address_zip_check":"",
            "brand":"American Express",
            "country":"US",
            "customer":"cus_54645",
            "cvc_check":"pass",
            "dynamic_last4":"",
            "exp_month":"23",
            "exp_year":"2078",
            "fingerprint":"Ldsddf",
            "funding":"credit",
            "last4":"1232",
            "metadata":[

            ],
            "name":"",
            "tokenization_method":""
         },
         {
            "id":"card_1Cfgfdgfdgfdgz",
            "object":"card",
            "address_city":"",
            "address_country":"",
            "address_line1":"",
            "address_line1_check":"",
            "address_line2":"",
            "address_state":"",
            "address_zip":"",
            "address_zip_check":"",
            "brand":"American Express",
            "country":"US",
            "customer":"cus_3434",
            "cvc_check":"pass",
            "dynamic_last4":"",
            "exp_month":"11",
            "exp_year":"2078",
            "fingerprint":"",
            "funding":"credit",
            "last4":"1611",
            "metadata":[

            ],
            "name":"test",
            "tokenization_method":""
         }
      ],
      "has_more":"",
      "total_count":"4",
      "url":"/v1/custfgdfg"
   },
   "subscriptions":{
      "object":"list",
      "data":[

      ],
      "has_more":"",
      "total_count":"0",
      "url":"/v1/cdsfdg"
   },
   "tax_info":"",
   "tax_info_verification":""
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.