{"id":98,"date":"2023-10-27T12:23:41","date_gmt":"2023-10-27T12:23:41","guid":{"rendered":"https:\/\/www.tvarwebu.cz\/blog\/?p=98"},"modified":"2023-10-27T12:45:24","modified_gmt":"2023-10-27T12:45:24","slug":"php-unset-arrayobject-in-foreach-skip-the-following-item-unexpected-behaviour","status":"publish","type":"post","link":"https:\/\/www.tvarwebu.cz\/blog\/2023\/10\/php-unset-arrayobject-in-foreach-skip-the-following-item-unexpected-behaviour\/","title":{"rendered":"PHP Unset ArrayObject in foreach skip the following item &#8211; unexpected behaviour"},"content":{"rendered":"<p>When you unset the item from array in foreach, all works as expected<\/p>\n<pre>$array = [1,2,3,4];\r\nforeach($array as $key =&gt; $value) {\r\n  if($value === 2) {\r\n    unset($array[$key]);\r\n    continue;\r\n  }\r\n  echo \"$value \";\r\n}<\/pre>\n<p>Will return following:<\/p>\n<pre>1 3 4<\/pre>\n<p>When you want to do the same with ArrayObject, you would expect exactly the same behaviour. Unfortunately, it is not the case.<\/p>\n<pre>class MainList extends \\ArrayObject {}\r\n$arrayListObject = new MainList([1,2,3,4]);\r\nforeach($arrayListObject as $key =&gt; $value) {\r\n  if($value === 2) {\r\n    unset($arrayListObject[$key]);\r\n    continue;\r\n  }\r\n  echo \"$value \";\r\n}<\/pre>\n<p>Will return following:<\/p>\n<pre>1 4<\/pre>\n<p><strong>See that the number 3 is missing in the response!!!<\/strong><br \/>\nThe reason for this is that the ArrayObject is behaving like object (not as an array) when looping through foreach.<\/p>\n<p>This could be very confusing when you expect that that the foreach will process all the items.<\/p>\n<p>The best option is to process it via<\/p>\n<pre>class MainList extends \\ArrayObject {}\r\n$arrayListObject = new MainList([1,2,3,4]);\r\nforeach($arrayListObject-&gt;getArrayCopy() as $key =&gt; $value) {\r\n  if($value === 2) {\r\n    unset($arrayListObject[$key]);\r\n    continue;\r\n  }\r\n  echo \"$value \";\r\n}<\/pre>\n<p>Will return following (as expected):<\/p>\n<pre>1 3 4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you unset the item from array in foreach, all works as expected $array = [1,2,3,4]; foreach($array as $key =&gt; $value) { if($value === 2) { unset($array[$key]); continue; } echo &#8220;$value &#8220;; } Will return following: 1 3 4 When you want to do the same with ArrayObject, you would expect exactly the same behaviour. &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/www.tvarwebu.cz\/blog\/2023\/10\/php-unset-arrayobject-in-foreach-skip-the-following-item-unexpected-behaviour\/\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-php","item-wrap"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":13,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tvarwebu.cz\/blog\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}