Help accessing object in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
interminable
Forum Newbie
Posts: 8
Joined: Thu Oct 01, 2009 4:22 pm

Help accessing object in an array

Post by interminable »

HI,

I can't seem to access the properties of an object I've returned. Can anyone help with this?
Thanks

Here's the dump (I've bolded the properties I can't get to):

Code: Select all

object(stdClass)#2 (1) {
  ["MakePaymentResult"]=>
  object(stdClass)#3 (7) {
    ["IsSuccess"]=>
    bool(true)
    ["ErrorMessage"]=>
    string(0) ""
    ["SubType"]=>
    string(19) "Payment"
    ["PsObject"]=>
    object(stdClass)#4 (1) {
      ["PsObject"]=>
      object(stdClass)#5 (8) {
       [b] ["Type"]=>[/b]
        string(4) "Moto"
       [b] ["PaymentId"]=>[/b]
        int(594494)
       [b] ["CustomerId"]=>[/b]
        int(714296)
       [b] ["FromAccountId"]=>[/b]
        int(306921)
      [b]  ["Amount"]=>[/b]
        string(3) "3.5"
       [b] ["Status"]=>[/b]
        string(10) "Authorized"
      [b]  ["PaymentDate"]=>[/b]
        string(19) "0001-01-01T00:00:00"
       [b] ["InvoiceId"]=>
        NULL[/b]
      }
    }
    ["TotalItems"]=>
    int(0)
    ["ItemsPerPage"]=>
    int(0)
    ["CurrentPage"]=>
    int(0)
  }
}
shouldn't echo $retval->MakePaymentResult->PsObject[0] work?
or even echo $retval->MakePaymentResult->PsObject[0]->PaymentDate; ?
Last edited by interminable on Fri Oct 09, 2009 3:34 pm, edited 1 time in total.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Help accessing object in an array

Post by JNettles »

Having fun with web services are we? Navigating the results can always be loads of fun. :wink:

I'm guessing this is just a typo but your dump doesn't have MakeCcPaymentResult anywhere in it, only MakePaymentResult. What I usually do in a case like this is to drill through the dump like so.....

var_dump($retval->MakePaymentResult)

....which will give you more focused result set of those properties.....

var_dump($retval->MakePaymentResult->PSObject)

.... and so on and so forth. Just keep drilling through the dump til you get to where you need.
interminable
Forum Newbie
Posts: 8
Joined: Thu Oct 01, 2009 4:22 pm

Re: Help accessing object in an array

Post by interminable »

Not a typo, it's correct in my local code, I've updated the code shown here.

What I'm asking is how do I access the info inside the array?

var_dump($retval->MakeCcPaymentResult->PsObject);

displays the object. I can't get any further in to access the properties of the object.

am I supposed to access it using PsObject[0] or perhaps a key like in a dictionary?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help accessing object in an array

Post by requinix »

Why do you think you need a [0]? The var_dump doesn't mention arrays anywhere.
interminable
Forum Newbie
Posts: 8
Joined: Thu Oct 01, 2009 4:22 pm

Re: Help accessing object in an array

Post by interminable »

My question doesn't involve var dump, I just used it to show what the structure of the object looks like.

My question is how do I get to the properties of the first element of an array.

As I said, I can get to the array object ($retval->MakePaymentResult->PSObject) (where PSobject is the entire array)

but I can't get to the elements inside of it

$retval->MakePaymentResult->PSObject[0] <---- doesnt work.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Help accessing object in an array

Post by Weiry »

Im just curious, have you tried running a print_r() return?

Code: Select all

print_r($retval->MakePaymentResult->PSObject)
If indeed it is an array, it should print all your array values. If for some reason no array values exist, then it would be obvious why [0] wont return anything.
The only other thing i could think of would be permissions to the array, but i really dont think that has anything to do with your problem.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Help accessing object in an array

Post by JNettles »

Did you try........
$retval->MakePaymentResult->PSObject->Type ? Don't forget you access property values with the -> indicator. That's about the best I can do just looking at your dump.
interminable
Forum Newbie
Posts: 8
Joined: Thu Oct 01, 2009 4:22 pm

Re: Help accessing object in an array

Post by interminable »

Ahhh... I see the problem now.

If there's only one object in the array, PHP throws out the whole array thing.

so var->var->var[0] just becomes var->var->var

This is kind of annoying, as the first way won't work if the array only has one item. Does anyone know a way to make PHP not do this automatically?

note: I'm not creating the array in the PHP code, it's returned by a webservice. not sure if that could have sway on anything.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help accessing object in an array

Post by requinix »

Nope, but is_array is your friend.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Help accessing object in an array

Post by superdezign »

interminable wrote:Ahhh... I see the problem now.

If there's only one object in the array, PHP throws out the whole array thing.
But.. it's an object. Not an array.

By the way, it looks like no one has noticed this... Your var_dump shows that there is a PsObject inside of your PsObject.
So, to get type, you'd use $retval->MakePaymentResult->PsObject->PsObject->Type
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Help accessing object in an array

Post by JNettles »

Web services can be a little touchy at times, and I always get a fresh headache just about every day navigating their results. It helps if you're the one writing the services too but even then they can be an exercise in keyboard-snapping frustration. What are these web services written in, if you don't mind my asking?
Post Reply