Page 1 of 1
Help accessing object in an array
Posted: Fri Oct 09, 2009 2:36 pm
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; ?
Re: Help accessing object in an array
Posted: Fri Oct 09, 2009 3:23 pm
by JNettles
Having fun with web services are we? Navigating the results can always be loads of fun.
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.
Re: Help accessing object in an array
Posted: Fri Oct 09, 2009 3:37 pm
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?
Re: Help accessing object in an array
Posted: Fri Oct 09, 2009 3:55 pm
by requinix
Why do you think you need a [0]? The var_dump doesn't mention arrays anywhere.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 9:17 am
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.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 9:23 am
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.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 10:25 am
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.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 3:22 pm
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.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 4:04 pm
by requinix
Nope, but
is_array is your friend.
Re: Help accessing object in an array
Posted: Mon Oct 12, 2009 4:12 pm
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
Re: Help accessing object in an array
Posted: Tue Oct 13, 2009 9:06 am
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?