{"id":2178,"date":"2019-10-14T11:40:07","date_gmt":"2019-10-14T06:10:07","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=2178"},"modified":"2019-10-14T11:40:07","modified_gmt":"2019-10-14T06:10:07","slug":"alternative-deprecated-php-each-function","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/","title":{"rendered":"Alternative to the Deprecated PHP &#8220;each&#8221; Function"},"content":{"rendered":"<p>Recently I have encountered an issue with my old codes. Those were running just fine earlier. Issue occurs while my server updated its PHP version to 7.2.<\/p>\n<p>Actually, this is common, with up-gradation to server configurations some old functions get deprecated, and in my case, it was PHP \u201ceach\u201d function.<\/p>\n<p>With PHP version 7.2 \u201ceach\u201d function got deprecated and hence my old codes throwing warnings.<\/p>\n<p>I have to modify my codes and replace the use of \u201ceach\u201d with relevant alternatives. Luckily my old codes have a modular structure and most of my logic is bound into some functions, hence I have to change only a few lines of code to remove the warnings.<\/p>\n<p>Here I am giving some examples of the use of \u201ceach\u201d function and alternative way to replicate them with PHP 7.2 compatible codes.<\/p>\n<h3>EACH usage:<\/h3>\n<p>(is_array($data)) ? list($data, $params) = each($data) : $params = NULL;<\/p>\n<h3>Alternative:<\/h3>\n<p>&nbsp;<\/p>\n<p>(is_array($data)) ? list($data, $params) = $this-&gt;altEach($data) : $params = NULL;<\/p>\n<p>function altEach(&amp;$data)<\/p>\n<p>{<\/p>\n<p>$key = key($data);<\/p>\n<p>$ret = ($key === null)? false: [$key, current($data), &#8216;key&#8217; =&gt; $key, &#8216;value&#8217; =&gt; current($data)];<\/p>\n<p>next($data);<\/p>\n<p>return $ret;<\/p>\n<p>}<\/p>\n<h3>EACH usage:<\/h3>\n<p>for(reset($data); $keyvalue = each($data);) {&#8230;}<\/p>\n<h3>Alternative:<\/h3>\n<p>&nbsp;<\/p>\n<p>foreach ($data as $key =&gt; $val) {<\/p>\n<p>$keyvalue = [$key, $val];<\/p>\n<p>}<\/p>\n<h3>EACH usage:<\/h3>\n<p>$data = array(&#8216;arrayInp&#8217; =&gt; array(), &#8216;intInp&#8217; =&gt; 2, &#8216;otherInp&#8217; =&gt; null);<\/p>\n<p>$output = each($data);<\/p>\n<h3>Alternative:<\/h3>\n<p>&nbsp;<\/p>\n<p>$data = array(&#8216;arrayInp&#8217; =&gt; array(), &#8216;intInp&#8217; =&gt; 2, &#8216;otherInp&#8217; =&gt; null);<\/p>\n<p>$output = [key($data), current($data)];<\/p>\n<h3>EACH usage:<\/h3>\n<p>$i = 0;<\/p>\n<p>reset($data);<\/p>\n<p>while( (list($key, $value) = each($data)) || $i &lt; 30 ) {<\/p>\n<p>\/\/ your code goes here<\/p>\n<p>$i++;<\/p>\n<p>}<\/p>\n<h3>Alternative:<\/h3>\n<p>&nbsp;<\/p>\n<p>reset($data);<\/p>\n<p>for ($i = 0; $i &lt; 30; $i++) {<\/p>\n<p>$key = key($data);<\/p>\n<p>$value = current($data);<\/p>\n<p>\/\/ your code goes here<\/p>\n<p>next($data);<\/p>\n<p>}<\/p>\n<p>Hope that will help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I have encountered an issue with my old codes. Those were running just fine earlier. Issue occurs while my server updated its PHP version to 7.2. Actually, this is common, with up-gradation to server configurations some old functions get deprecated, and in my case, it was PHP \u201ceach\u201d function. With PHP version 7.2 \u201ceach\u201d [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":2198,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,22,18],"tags":[213,210,211,214,71,35,212],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Alternative to the Deprecated PHP &quot;each&quot; Function<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Alternative to the Deprecated PHP &quot;each&quot; Function\" \/>\n<meta property=\"og:description\" content=\"Recently I have encountered an issue with my old codes. Those were running just fine earlier. Issue occurs while my server updated its PHP version to 7.2. Actually, this is common, with up-gradation to server configurations some old functions get deprecated, and in my case, it was PHP \u201ceach\u201d function. With PHP version 7.2 \u201ceach\u201d [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-14T06:10:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/10\/pexels-photo-2194062.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1880\" \/>\n\t<meta property=\"og:image:height\" content=\"1253\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/10\/pexels-photo-2194062.jpeg\",\"width\":1880,\"height\":1253,\"caption\":\"each programming\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/\",\"name\":\"Alternative to the Deprecated PHP \\\"each\\\" Function\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/#primaryimage\"},\"datePublished\":\"2019-10-14T06:10:07+00:00\",\"dateModified\":\"2019-10-14T06:10:07+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/f584a343b2568016931c94b204e992fb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/alternative-deprecated-php-each-function\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/f584a343b2568016931c94b204e992fb\",\"name\":\"Rajarshi Banerjee\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7bf3399ae5b90592781bc24506579145?s=96&r=g\",\"caption\":\"Rajarshi Banerjee\"},\"description\":\"Over 7 years of experience in web development.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/2178"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=2178"}],"version-history":[{"count":8,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions"}],"predecessor-version":[{"id":2199,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions\/2199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/2198"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}