{"id":7240,"date":"2021-07-31T13:20:06","date_gmt":"2021-07-31T13:20:06","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7240"},"modified":"2021-07-31T13:20:08","modified_gmt":"2021-07-31T13:20:08","slug":"tutorial-for-for-each-loop","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/","title":{"rendered":"Tutorial for &#8220;For each&#8221; loop"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What is For each Loops?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the <code><span class=\"has-inline-color has-vivid-red-color\"><strong>for each<\/strong><\/span><\/code> loops to cycle through arrays. At each pass, the value of the current array element is set to $value. In the next pass, the array pointer is moved one by one. The <strong><code><span class=\"has-inline-color has-vivid-red-color\">for each<\/span><\/code><\/strong> loop is only used to loop around each key\/values pair in an array. Only arrays are supported.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:-<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"has-inline-color has-vivid-red-color\">foreach ($array as $value) {\n  code to be executed;\n}<\/span><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Here are some of the Foreach Loops:-<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u201cforeach(\u2026){\u2026}\u201d<\/strong>\u00a0is the foreach php loop block code<\/li><li><strong>\u201c$array_data\u201d<\/strong>\u00a0is the array variable to be looped through<\/li><li><strong>\u201c$array_value \u201c<\/strong>\u00a0is the temporary variable that holds the current array item values.<\/li><li>\u201c<strong>block of code<\/strong>\u2026\u201d is the piece of code that operates on the array values<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/2013\/04\/for_loop_flowchart.png\" alt=\"PHP Loop and  Control Structures\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example1:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/AdarshAshwani\/526c51b5fccc8e63659f0388b250d957.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:-<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/05\/image-46.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example2:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/AdarshAshwani\/a622128bd15bef37bfb48b6a466212b1.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:-<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/05\/image-47.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is For each Loops? Use the for each loops to cycle through arrays. At each pass, the value of [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":7187,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5388],"tags":[5784,5785,5786,5258,5737,177,213,5788,5730,670,5787],"class_list":["post-7240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-for-each","tag-for-each-loop","tag-for-each-loops","tag-loops","tag-loops-in-php","tag-php","tag-tutorial","tag-tutorial-for-for-each-loop","tag-tutorials-for-php-loops","tag-what","tag-what-is-for-each-loops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial for &quot;For each&quot; loop - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial for &quot;For each&quot; loop - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"What is For each Loops? Use the for each loops to cycle through arrays. At each pass, the value of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/profile.php?id=100003935402658\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-31T13:20:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-31T13:20:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"666\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwani Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwani Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/\"},\"author\":{\"name\":\"Ashwani Kumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"headline\":\"Tutorial for &#8220;For each&#8221; loop\",\"datePublished\":\"2021-07-31T13:20:06+00:00\",\"dateModified\":\"2021-07-31T13:20:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/\"},\"wordCount\":124,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/Loops-for-PHP.jpg\",\"keywords\":[\"For each\",\"For each loop\",\"For each Loops\",\"loops\",\"loops in PHP\",\"Php\",\"Tutorial\",\"Tutorial for &quot;For each&quot; loop\",\"Tutorials for PHP Loops\",\"What\",\"What is For each Loops\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/\",\"name\":\"Tutorial for \\\"For each\\\" loop - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/Loops-for-PHP.jpg\",\"datePublished\":\"2021-07-31T13:20:06+00:00\",\"dateModified\":\"2021-07-31T13:20:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/Loops-for-PHP.jpg\",\"contentUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/Loops-for-PHP.jpg\",\"width\":666,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/tutorial-for-for-each-loop\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial for &#8220;For each&#8221; loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\",\"name\":\"Ashwani Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"caption\":\"Ashwani Kumar\"},\"description\":\"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100003935402658\",\"https:\\\/\\\/www.instagram.com\\\/adarshashwani\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/adarsh-ashwani-a75b23130\\\/\"],\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/ashwani\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial for \"For each\" loop - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial for \"For each\" loop - scmGalaxy","og_description":"What is For each Loops? Use the for each loops to cycle through arrays. At each pass, the value of [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/","og_site_name":"scmGalaxy","article_author":"https:\/\/www.facebook.com\/profile.php?id=100003935402658","article_published_time":"2021-07-31T13:20:06+00:00","article_modified_time":"2021-07-31T13:20:08+00:00","og_image":[{"width":666,"height":300,"url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg","type":"image\/jpeg"}],"author":"Ashwani Kumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ashwani Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/"},"author":{"name":"Ashwani Kumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"headline":"Tutorial for &#8220;For each&#8221; loop","datePublished":"2021-07-31T13:20:06+00:00","dateModified":"2021-07-31T13:20:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/"},"wordCount":124,"commentCount":0,"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg","keywords":["For each","For each loop","For each Loops","loops","loops in PHP","Php","Tutorial","Tutorial for &quot;For each&quot; loop","Tutorials for PHP Loops","What","What is For each Loops"],"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/","name":"Tutorial for \"For each\" loop - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#primaryimage"},"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg","datePublished":"2021-07-31T13:20:06+00:00","dateModified":"2021-07-31T13:20:08+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#primaryimage","url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg","contentUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/Loops-for-PHP.jpg","width":666,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/tutorial-for-for-each-loop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Tutorial for &#8220;For each&#8221; loop"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31","name":"Ashwani Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","caption":"Ashwani Kumar"},"description":"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.","sameAs":["https:\/\/www.facebook.com\/profile.php?id=100003935402658","https:\/\/www.instagram.com\/adarshashwani\/","https:\/\/www.linkedin.com\/in\/adarsh-ashwani-a75b23130\/"],"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/ashwani\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7240"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7240\/revisions"}],"predecessor-version":[{"id":7244,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7240\/revisions\/7244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media\/7187"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}