{"id":1048,"date":"2022-08-30T15:11:31","date_gmt":"2022-08-30T15:11:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/php-codeigniter-for-loop-undefined-offset-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:31","modified_gmt":"2022-08-30T15:11:31","slug":"php-codeigniter-for-loop-undefined-offset-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-codeigniter-for-loop-undefined-offset-collection-of-common-programming-errors\/","title":{"rendered":"PHP\/Codeigniter &#8211; For Loop [Undefined offset]-Collection of common programming errors"},"content":{"rendered":"<p>By using a <code>for<\/code> loop you are assuming that the keys of the array are contiguous, which they may not be. You are also assuming that every second level array has a <code>full_path<\/code> key, which it may not. Use <code>foreach<\/code> instead, and do an <code>isset()<\/code> check on the <code>full_path<\/code> key:<\/p>\n<pre><code>foreach ($status as $item)\n{\n\n    if (!isset($item['full_path'])) continue;\n\n    $conf = array(\n        'source_image' =&gt; $item['full_path'],\n        'new_image' =&gt; $this-&gt;upload_path . '\/thumbs',\n        'maintain_ratio' =&gt; true,\n        'width' =&gt; 200,\n        'height' =&gt; 200\n    );\n\n    $this-&gt;load-&gt;library('image_lib', $conf);\n    $this-&gt;image_lib-&gt;resize();\n\n    $this-&gt;image_lib-&gt;clear(); \/\/ complete reset    \n    $this-&gt;image_lib-&gt;initialize($conf); \/\/ complete reset\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:19:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>By using a for loop you are assuming that the keys of the array are contiguous, which they may not be. You are also assuming that every second level array has a full_path key, which it may not. Use foreach instead, and do an isset() check on the full_path key: foreach ($status as $item) { [&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-1048","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1048","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=1048"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1048\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}