{"id":659,"date":"2022-10-17T08:34:42","date_gmt":"2022-10-17T08:34:42","guid":{"rendered":"https:\/\/info.juliusgoh.life\/?p=659"},"modified":"2022-10-17T08:36:51","modified_gmt":"2022-10-17T08:36:51","slug":"rerun-an-array-even-if-the-loop-will-change-the-array-elements","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=659","title":{"rendered":"Rerun an array even if the loop will change the array elements"},"content":{"rendered":"<p>I have this problem where i will need to loop an array but while looping it , i will add elements into array and i need to loop that data as well. Below are an example that you can do so:-<\/p>\n<pre>\r\n    $array = array('1','1.5','3','5','7');\r\n    $count = count($array);\r\n    for($i=0; $i<$count;$i++)\r\n    {\r\n        $number = $array[$i];\r\n        echo $number.\"&lt;br&gt;\";\r\n        if($number == '3')\r\n        {\r\n            unset($array[$i]);\r\n            $array[] = '2';\r\n            $array[] = '4';\r\n            sort($array);\r\n            $array = array_values($array);\r\n            $count = count($array);\r\n            $i = -1;\r\n        }\r\n        elseif($number == '4')\r\n        {\r\n            unset($array[$i]);\r\n            $array[] = '1';\r\n            $array[] = '10';\r\n            sort($array);\r\n            $array = array_values($array);\r\n            $count = count($array);\r\n            $i = -1;\r\n        }\r\n        else\r\n        {\r\n            unset($array[$i]);\r\n\r\n        }\r\n        echo \"&lt;hr&gt;\";\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have this problem where i will need to loop an array but while looping it , i will add elements into array and i need to loop that data as well. Below are an example that you can do so:- $array = array(&#8216;1&#8242;,&#8217;1.5&#8242;,&#8217;3&#8242;,&#8217;5&#8242;,&#8217;7&#8217;); $count = count($array); for($i=0; $i<\/p>\n","protected":false},"author":1,"featured_media":660,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/659"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=659"}],"version-history":[{"count":5,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions\/665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/660"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}