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



Howto Fix Fatal Error: Cannot Use Object Of Type StdClass As Array In PHP
 (380) Click. Work. Collect
Howto Fix Fatal Error: Cannot Use Object Of Type StdClass As Array In PHP
Post Description:
Post Tags: howto, fix, fatal, error, cannot, use, object, of, type, stdclass, as, array, in, php
This Post Has Been Viewed 2048 Times Since Tue Jul 17, 2007 9:14 pm Posted By hostman with 6 replies
Next Post »» How To Fix Fatal error: Class 'SoapVar' not found in PHP Apache Linux Server
Howto 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


Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About Howto Fix Fatal Error: Cannot Use Object Of Type StdClass As Array In PHP




:: 1 :: #46521 - Reply By Gregg On Fri Mar 21, 2008 1:11 pm
i was hoping this would be my holy grail answer to this problem but alas i still can't get any output.
:: 2 :: #47501 - Reply By Gregg On Fri Mar 28, 2008 12:44 pm
ok-got it working. your example is great if you are returning a single object,
now how would you do a collection of the same objects?
:: 3 :: #50388 - Reply By Chris On Wed Apr 16, 2008 9:32 am
thank you so much for posting this. it was exactly the answer to my problem.
:: 4 :: #58039 - Reply By James On Mon Jun 16, 2008 4:20 pm
exactly the solution i needed, thanks!
:: 5 :: #59953 - Reply By jax On Tue Jul 01, 2008 11:58 am
dear hostman.
it was very useful to me, in one of my projects. saved lot of time. thank you very much.
:: 6 :: #61105 - Reply By Mayart On Thu Jul 10, 2008 7:08 am
exactly what i needed - thanks a lot :)