{"id":690,"date":"2022-11-16T12:03:21","date_gmt":"2022-11-16T12:03:21","guid":{"rendered":"https:\/\/www.devopstrainer.in\/blog\/?p=690"},"modified":"2022-11-16T12:51:49","modified_gmt":"2022-11-16T12:51:49","slug":"how-can-we-create-foreach-loop-with-a-numeric-array-in-php","status":"publish","type":"post","link":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/","title":{"rendered":"How can we create foreach loop with a numeric array in PHP?"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg\" alt=\"\" class=\"wp-image-693\" width=\"749\" height=\"421\" srcset=\"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg 1024w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-300x169.jpg 300w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-768x432.jpg 768w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-678x381.jpg 678w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop.jpg 1400w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><figcaption><strong><em>Foreach loop in Php<\/em><\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>In the query, we will discuss about&#8221; how can you create foreach loop with a numeric array in Php?&#8221;. Here, we discuss the foreach loop with a numeric array or you may say index array also. what about the foreach loop? you can use foreach loop merely on an array. For pairing through key values, we apply of foreach loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is foreach in PHP?<\/h2>\n\n\n\n<p>The foreach() method is used in PHP which works on arrays and is used for creating a loop through each key\/value pair in an array. it allows a loop through an element in indexed &amp; associated arrays and also run a block of codes for each element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of foreach() loop:<\/h2>\n\n\n\n<p>The syntax for indexed arrays is as given in the below-mentioned block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach($array_name as $value){\nblock of statement;\n}<\/code><\/pre>\n\n\n\n<p>Here, \u201c<strong>Name<\/strong>\u201d is the required parameter. It is the array or the variable containing the array. \u201c<strong>$value<\/strong>\u201d is a variable that stores the current element in each iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code &amp; Discussion:<\/h2>\n\n\n\n<p>In this paragraph, we look at how the foreach() function works on an indexed array through its parameter and element-containing variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP Foreach() on Indexed\/numeric arrays:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php  \n$colors = array(\"apple\", \"guava\", \"mango\", \"apricot\"); \n\nforeach ($colors as $value) {\n  echo \"$value &lt;br&gt;\";\n}\n?&gt; <\/code><\/pre>\n\n\n\n<p>The output of the above code snippet would be:<\/p>\n\n\n\n<p>Apple<br>guava<br>mango<br>apricot<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the query, we will discuss about&#8221; how can you create foreach loop with a numeric array in Php?&#8221;. Here, we discuss the foreach loop with a&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145,230,165,113],"tags":[268,269,270,272,271],"class_list":["post-690","post","type-post","status-publish","format-standard","hentry","category-array","category-elements","category-function","category-php","tag-foreach-loop","tag-numeric-array-in-php","tag-php-foreach","tag-syntax-for-indexed-arrays","tag-syntax-of-foreach-loop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps<\/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.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps\" \/>\n<meta property=\"og:description\" content=\"In the query, we will discuss about&#8221; how can you create foreach loop with a numeric array in Php?&#8221;. Here, we discuss the foreach loop with a...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps | SRE | DevSecOps\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-16T12:03:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-16T12:51:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg\" \/>\n<meta name=\"author\" content=\"rahulkr kr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rahulkr kr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/\"},\"author\":{\"name\":\"rahulkr kr\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"headline\":\"How can we create foreach loop with a numeric array in PHP?\",\"datePublished\":\"2022-11-16T12:03:21+00:00\",\"dateModified\":\"2022-11-16T12:51:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/\"},\"wordCount\":213,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/php-foreach-loop-1024x576.jpg\",\"keywords\":[\"foreach() loop\",\"numeric array in PHP\",\"PHP Foreach()\",\"syntax for indexed arrays\",\"Syntax of foreach() loop\"],\"articleSection\":[\"Array\",\"Elements\",\"Function\",\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/\",\"name\":\"How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/php-foreach-loop-1024x576.jpg\",\"datePublished\":\"2022-11-16T12:03:21+00:00\",\"dateModified\":\"2022-11-16T12:51:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/php-foreach-loop.jpg\",\"contentUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/php-foreach-loop.jpg\",\"width\":1400,\"height\":788},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can we create foreach loop with a numeric array in PHP?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\",\"name\":\"DevOps | SRE | DevSecOps\",\"description\":\"Automation means Cost, Quality, Time\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\",\"name\":\"rahulkr kr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g\",\"caption\":\"rahulkr kr\"},\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/author\\\/rahulkr\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps","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.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps","og_description":"In the query, we will discuss about&#8221; how can you create foreach loop with a numeric array in Php?&#8221;. Here, we discuss the foreach loop with a...","og_url":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/","og_site_name":"DevOps | SRE | DevSecOps","article_published_time":"2022-11-16T12:03:21+00:00","article_modified_time":"2022-11-16T12:51:49+00:00","og_image":[{"url":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg","type":"","width":"","height":""}],"author":"rahulkr kr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rahulkr kr","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#article","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/"},"author":{"name":"rahulkr kr","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"headline":"How can we create foreach loop with a numeric array in PHP?","datePublished":"2022-11-16T12:03:21+00:00","dateModified":"2022-11-16T12:51:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/"},"wordCount":213,"commentCount":0,"image":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg","keywords":["foreach() loop","numeric array in PHP","PHP Foreach()","syntax for indexed arrays","Syntax of foreach() loop"],"articleSection":["Array","Elements","Function","PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/","url":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/","name":"How can we create foreach loop with a numeric array in PHP? - DevOps | SRE | DevSecOps","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop-1024x576.jpg","datePublished":"2022-11-16T12:03:21+00:00","dateModified":"2022-11-16T12:51:49+00:00","author":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"breadcrumb":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#primaryimage","url":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop.jpg","contentUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/php-foreach-loop.jpg","width":1400,"height":788},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopstrainer.in\/blog\/how-can-we-create-foreach-loop-with-a-numeric-array-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopstrainer.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How can we create foreach loop with a numeric array in PHP?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopstrainer.in\/blog\/#website","url":"https:\/\/www.devopstrainer.in\/blog\/","name":"DevOps | SRE | DevSecOps","description":"Automation means Cost, Quality, Time","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopstrainer.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d","name":"rahulkr kr","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79531cbaf0b831cebc9631f6ac28f21fb3fb0dc2615eeecdeeec43038b513473?s=96&d=mm&r=g","caption":"rahulkr kr"},"url":"https:\/\/www.devopstrainer.in\/blog\/author\/rahulkr\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":4,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions\/695"}],"wp:attachment":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}