Wallpapers Forums Login

How to Fix Fatal Error: Cannot Use Object Of Type StdClass As Array In PHP

ok, today i was working on a script and i got this error when i tried to display the information from an array.

now, i am not an expert on arrays, but ususally i can figure them out, but this error i've never seen.

at the end of the script there was this variabled: $response

so when i echo it, it didn't show anything,
echo $response;


then i thought, maybe its an array so i put this code:
echo "<pre>";
var_dump($response);
echo "</pre>";


it worked, i got these results on my browser: OUTPUT:
object(stdClass)#6 (9) {
["Timestamp"]=>
string(20) "2007-07-18T04:50:52Z"
["Ack"]=>
string(7) "Success"
["CorrelationID"]=>
string(13) "28c01821ba2c2"
["Version"]=>
string(8) "2.400000"
["Build"]=>
string(6) "1.0006"
["Amount"]=>
object(stdClass)#7 (2) {
["_"]=>
string(5) "40.00"
["currencyID"]=>
string(3) "USD"
}
["AVSCode"]=>
string(1) "X"
["CVV2Code"]=>
string(1) "M"
["TransactionID"]=>
string(17) "14E028078XJ827961UX"
}


ok, i didn't want to show this to my users, instead, i wanted to show only the TransactionID part so i put this code instead:
echo $response['TransactionID'][17];


that's when i got this error:


Fatal error: Cannot use object of type stdClass as array in C:windows-htdocs-cart.php on line 535


ok, so how do you fix this error,

i looked it up on google and found some post and forums where it say to upgrade my php or change the softwar or that i have a bad script. well, i finally figured out. it turns out that $response is actually an object. i have no idea what are objects in PHP but i came across an article about ojects and i found out that if you want to display it or show it with echo or print, you actually have to use the a different format. so in my example above, if i wanted to show the value TransactionID, this is how i would do it:

CODE
echo $response->TransactionID;


OUTPUT:
14E028078XJ827961UX


Perfect, just how i wanted.

well i hope this helps someone.

Thanks to webune.com for helping with this question. i have a dedicated server with them and they are very helpful with their support.

thanks webune.com

Jarrod Sun May 13, 2012
Thanks a lot!
harsh Tue May 08, 2012
thanks so much helped a lot
thanks Thu Apr 26, 2012
thanks man!
sulabh Wed Apr 18, 2012
thanx a lot
David Wed Apr 18, 2012
Thank you, bro, your tip saves me a lot of time...great!
AKmain Tue Apr 17, 2012
Thank you very much!
John Fri Mar 30, 2012
Awesome and thanks! I had to tweak mine a tad bit because my situation was a little diff but it worked!
Matt Thu Mar 29, 2012
Thank you so much!
CoursesWeb Tue Mar 27, 2012
Hi,
Just thank you for this article, it helped me, like many other visitors, to solve a problem in which i was accessed an object as an array.
sonia Thu Mar 15, 2012
thanks a lot
Jan Mon Feb 20, 2012
Thanks from germany.

Jan
thanks Tue Jan 24, 2012
JUST WANTED TO SAY THANKS BUT I HAVE TO FILL OUT THOUSAND FIELDS AND CHECKBOXES JUST TO SAY THANK YOU. LOVE.
deak Wed Jan 11, 2012
THX a lot it helped me out!
Akhilesh Fri Dec 16, 2011
Thanks a ton bro!!! Great job indeed.
Fran Fri Nov 25, 2011
this helped a lot. I'm not the best at php. I built a simple twitter app, the api changed a bit and everything broke. this helped me fix it. Thanks.

Related Content

Information

Share

wallpaperama | Wallpapers | Forums | Terms Of Service

copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5