{"id":1243,"date":"2022-08-30T15:14:46","date_gmt":"2022-08-30T15:14:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/proper-way-to-get-attribute-from-json-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:46","modified_gmt":"2022-08-30T15:14:46","slug":"proper-way-to-get-attribute-from-json-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/proper-way-to-get-attribute-from-json-collection-of-common-programming-errors\/","title":{"rendered":"Proper way to get attribute from json-Collection of common programming errors"},"content":{"rendered":"<pre><code> \n              <br \/><br \/><br \/>\n            \n<\/code><\/pre>\n<p>There is an error in the code above: undefined offset on the line with the if. I guess it is because <code>$getlikes[$key]['type']<\/code> is wrong. What would be the proper code to get all the objects with the <code>type<\/code> attribute being &#8220;like&#8221;?<\/p>\n<p>Json can be downloaded from here<\/p>\n<ol>\n<li>\n<p>This really depends on the content of your .json object. But most likely the undefined offset is being caused because not all $getlikes[$key] has an array key of &#8216;type&#8217;. Validate to make sure it exists before you compare. Try<\/p>\n<pre><code>foreach ($get_json_values as $key=&gt;$getlikes) { \n    if(!isset($getlikes['type'])) continue;\n        if($getlikes['type']=='like') {\n    ....\n<\/code><\/pre>\n<p>EDIT:<\/p>\n<p>Based on your .json file you were just adding an additional key which was unneeded. Changed the code to reflect the correct call. Still not a bad idea to check if a value is set. Try a var_dump($getlikes) or on the file you are using to get a good look at what you are iterating over.<\/p>\n<\/li>\n<li>\n<p>Given the JSON string it&#8217;s clear what you&#8217;re doing wrong. $get_json_values is a list with no indexes, you&#8217;re doubling up on the key, hard to explain. Do this instead.<\/p>\n<pre><code>foreach ($get_json_values as $getlikes) { \n    if($getlikes['type']=='like') {\n<\/code><\/pre>\n<p>If you were to use $key, you&#8217;d use it like this<\/p>\n<pre><code>foreach ($get_json_values as $key=&gt;$getlikes) { \n    if($get_json_values[$key]['type']=='like') {\n<\/code><\/pre>\n<p>but you wouldn&#8217;t do that anyway. Foreach extracts the elements one by one.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>There is an error in the code above: undefined offset on the line with the if. I guess it is because $getlikes[$key][&#8216;type&#8217;] is wrong. What would be the proper code to get all the objects with the type attribute being &#8220;like&#8221;? Json can be downloaded from here This really depends on the content of your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1243"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}