{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Fatal error: Cannot use object of type stdClass as array in<br\/>C:\\Users\\Wiki\\software\\wikitechy.php on line 108<\/code><\/pre> <\/div>\n<p>We tried to do: $result[&#8216;context&#8217;] 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">var_dump($result-&gt;context);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">var_dump($result-&gt;{&#039;from-date&#039;});<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">var_dump($result-&gt;{&#039;from-date&#039;});<\/code><\/pre> <\/div>\n<ul>\n<li>If we need an array we can do the following:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result = json_decode($json, true);<\/code><\/pre> <\/div>\n<ul>\n<li>Or cast the object to an array:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result = (array) json_decode($json);<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result = json_decode($data, true);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$my_array = json_decode($my_json, true);<\/code><\/pre> <\/div>\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-&gt;key,<\/li>\n<li>But if we call json_decode($somestring, true) we will get an array and can access like $array[&#8216;key&#8217;]<\/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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$array = (array)$stdClass;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>function:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )<\/code><\/pre> <\/div>\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 -&gt; since its an object.<\/p>\n<p>Change our code from:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result[&#039;context&#039;];<\/code><\/pre> <\/div>\n<p>To:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result-&gt;context;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$a = (array)$object;<\/code><\/pre> <\/div>\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 -&gt; Operator.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$value = $object-&gt;key;<\/code><\/pre> <\/div>\n<ul>\n<li>Use multiple keys to extract the sub elements incase if the object has nested arrays.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$value = $object-&gt;key1-&gt;key2-&gt;key3...;<\/code><\/pre> <\/div>\n<p>Their are many options to print_r() like var_dump(); and var_export();<\/p>\n[ad type=&#8221;banner&#8221;]\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: We tried to do: $result[&#8216;context&#8217;] where $result has the data returned by json_decode() php json SOLUTION 1: The function json_decode() returns an object by default. Access the [&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}]}}