{"id":1837,"date":"2017-03-23T15:25:29","date_gmt":"2017-03-23T09:55:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1837"},"modified":"2017-03-28T18:24:34","modified_gmt":"2017-03-28T12:54:34","slug":"cannot-use-object-of-type-std-class-as-array","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cannot-use-object-of-type-std-class-as-array\/","title":{"rendered":"[ Solved &#8211; 8 Answers ] PHP &#8211; Cannot use object of type stdClass as array"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>We get a strange error using json_decode(). It decode correctly the data, but when we try to access info inside the array we get the following:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dFatal%20error%3A%20Cannot%20use%20object%20of%20type%20stdClass%20as%20array%20in%0AC%3A%5CUsers%5CWiki%5Csoftware%5Cwikitechy.php%20on%20line%20108%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>We tried to do: $result[\u2018context\u2019] where $result has the data returned by json_decode()<\/p>\n<p>php json<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>The function json_decode() returns an object by default.<\/p>\n<p>Access the data :<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar_dump(%24result-%3Econtext)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>If we have identifiers like from-date (the hyphen would cause a PHP error when using the above method) we have to write the following:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar_dump(%24result-%3E%7B\u2019from-date\u2019%7D)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If we have identifiers like from-date (the hyphen would cause a PHP error when using the above method) we have to write the following:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar_dump(%24result-%3E%7B\u2019from-date\u2019%7D)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If we need an array we can do the following:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result%20%3D%20json_decode(%24json%2C%20true)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Or cast the object to an array:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result%20%3D%20(array)%20json_decode(%24json)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>Use the second parameter of json_decode to make it return an array:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result%20%3D%20json_decode(%24data%2C%20true)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>Use true as the second parameter to json_decode.<\/li>\n<li>This will decode the json into an associative array instead of stdObject instances:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24my_array%20%3D%20json_decode(%24my_json%2C%20true)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>If we call json_decode($somestring) we will get an Object and we need to access like $object->key,<\/li>\n<li>But if we call json_decode($somestring, true) we will get an array and can access like $array[\u2018key\u2019]<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<p>We can convert stdClass object to array :<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24array%20%3D%20(array)%24stdClass%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>function:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dmixed%20json_decode%20(%20string%20%24json%20%5B%2C%20bool%20%24assoc%20%3D%20false%20%5B%2C%20int%20%24depth%20%3D%20512%20%5B%2C%20int%20%24options%20%3D%200%20%5D%5D%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>When param is false, which is default, it will return an appropriate php type.<\/li>\n<li>When param is true, it will return associative arrays.<\/li>\n<li>It will return NULL on error.<\/li>\n<li>If we want to fetch value through array, set assoc to true.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<p>we must access it using -> since its an object.<\/p>\n<p>Change our code from:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result%5B\u2019context\u2019%5D%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>To:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result-%3Econtext%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>print_r \u2014 Prints human-readable information about a variable<\/li>\n<li>When we use json_decode();, we get an object of type stdClass as return type.<\/li>\n<li>The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). There are two ways to deal with this.<\/li>\n<\/ul>\n<p>1. Cast the object to array.<br \/>\nThis can be achieved as follows.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24a%20%3D%20(array)%24object%3B%0A\u201d message=\u201dphp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>2. By accessing the key of the Object<br \/>\nwhen we use json_decode(); function, it returns an Object of stdClass. we can access the elements of the object with the help of -> Operator.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24value%20%3D%20%24object-%3Ekey%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Use multiple keys to extract the sub elements incase if the object has nested arrays.<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24value%20%3D%20%24object-%3Ekey1-%3Ekey2-%3Ekey3\u2026%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Their are many options to print_r() like var_dump(); and var_export();<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : We get a strange error using json_decode(). It decode correctly the data, but when we try to access info inside the array we get the following: [pastacode lang=\u201dphp\u201d manual=\u201dFatal%20error%3A%20Cannot%20use%20object%20of%20type%20stdClass%20as%20array%20in%0AC%3A%5CUsers%5CWiki%5Csoftware%5Cwikitechy.php%20on%20line%20108%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] We tried to do: $result[\u2018context\u2019] where $result has the data returned by json_decode() php json SOLUTION 1: The function json_decode() [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[3889,3893,3887,3891,3888,3892,3886,1741,3890,3885],"class_list":["post-1837","post","type-post","status-publish","format-standard","hentry","category-php","tag-cannot-use-object-of-type-stdclass-as-array","tag-cannot-use-object-of-type-stdclass-as-array-json","tag-cannot-use-object-of-type-stdclass-as-array-in","tag-cannot-use-object-of-type-stdclass-as-array-magento","tag-cannot-use-object-of-type-stdclass-as-arrayphp","tag-json-decode-fatal-error-cannot-use-object-of-type-stdclass-as-array-in","tag-json_decode-to-array","tag-php","tag-php-cannot-use-object-of-type-stdclass-as-array","tag-what-is-the-correct-json-content-type"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1837"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1837\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}