{"id":542,"date":"2022-11-03T12:17:41","date_gmt":"2022-11-03T12:17:41","guid":{"rendered":"https:\/\/www.devopstrainer.in\/blog\/?p=542"},"modified":"2022-11-04T05:55:58","modified_gmt":"2022-11-04T05:55:58","slug":"how-to-traverse-an-array-in-php-and-print","status":"publish","type":"post","link":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/","title":{"rendered":"How to traverse an array in PHP and print?"},"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\/traverse-array-in-php-1024x576.jpg\" alt=\"\" class=\"wp-image-543\" width=\"751\" height=\"422\" srcset=\"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-1024x576.jpg 1024w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-300x169.jpg 300w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-768x432.jpg 768w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-678x381.jpg 678w, https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php.jpg 1280w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><figcaption><strong><em>Traverse Array in PHP<\/em><\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>The most common task with arrays is to do something with every element\u2014for instance, sending mail to each element of an array of addresses, updating each file in an array of filenames, or adding up each element of an array of prices. There are several ways to traverse arrays in PHP, and the one you choose will depend on your data values and the task you&#8217;re performing.<\/p>\n\n\n\n<p>The foreach Construct<br>The most common way to loop over elements of an array is to use the foreach construct:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> $addresses = array('hack@pointpro.net', 'abroad@example.com'); \n   foreach ($addresses as $value) \n    echo \"Processing $value\\n&lt;br \/&gt;\";  <\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Processing hack@pointpro.net<br>Processing abroad@example.com<\/p>\n\n\n\n<p>PHP executes the body of the loop (the echo statement) once for each element of $addresses in turn, with $value set to the current element. Elements will be processed by their internal order. An alternative form of foreach gives you access to the current key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$person = array('name' =&gt; 'Alex', 'age' =&gt; 35, 'wife' =&gt; 'Ally'); \n    foreach ($person as $k =&gt; $v) \n     echo \"Alex's $k is $v\\n&lt;br \/&gt;\";<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>Alex&#8217;s name is Alex<br>Alex&#8217;s age is 35<br>Alex&#8217;s wife is Ally<\/p>\n\n\n\n<p>In this case, the key for each element is placed in $k and the corresponding value is placed in $v. The foreach construct does not operate on the array itself, but rather on a copy of it.<br>You can insert or delete elements in the body of a foreach loop, safe in the knowledge that the loop won&#8217;t attempt to process the deleted or inserted elements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most common task with arrays is to do something with every element\u2014for instance, sending mail to each element of an array of addresses, updating each file&#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,239,230,113,238,240],"tags":[147,242,244,245,241,243],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-array","category-echo-statement","category-elements","category-php","category-print","category-traverse","tag-array-in-php","tag-echo-statement","tag-element-of-an-array","tag-foreach-construct","tag-traverse-an-array-in-php","tag-traverse-arrays"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to traverse an array in PHP and print? - 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-to-traverse-an-array-in-php-and-print\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to traverse an array in PHP and print? - DevOps | SRE | DevSecOps\" \/>\n<meta property=\"og:description\" content=\"The most common task with arrays is to do something with every element\u2014for instance, sending mail to each element of an array of addresses, updating each file...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps | SRE | DevSecOps\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-03T12:17:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-04T05:55:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-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-to-traverse-an-array-in-php-and-print\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/\"},\"author\":{\"name\":\"rahulkr kr\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"headline\":\"How to traverse an array in PHP and print?\",\"datePublished\":\"2022-11-03T12:17:41+00:00\",\"dateModified\":\"2022-11-04T05:55:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/\"},\"wordCount\":235,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/traverse-array-in-php-1024x576.jpg\",\"keywords\":[\"Array in PHP\",\"echo statement\",\"element of an array\",\"foreach Construct\",\"traverse an array in PHP\",\"traverse arrays\"],\"articleSection\":[\"Array\",\"Echo Statement\",\"Elements\",\"PHP\",\"Print\",\"Traverse\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/\",\"name\":\"How to traverse an array in PHP and print? - DevOps | SRE | DevSecOps\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/traverse-array-in-php-1024x576.jpg\",\"datePublished\":\"2022-11-03T12:17:41+00:00\",\"dateModified\":\"2022-11-04T05:55:58+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/#\\\/schema\\\/person\\\/e9fd1d88de76754aa189257cd197394d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/traverse-array-in-php.jpg\",\"contentUrl\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/traverse-array-in-php.jpg\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/how-to-traverse-an-array-in-php-and-print\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopstrainer.in\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to traverse an array in PHP and print?\"}]},{\"@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 to traverse an array in PHP and print? - 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-to-traverse-an-array-in-php-and-print\/","og_locale":"en_US","og_type":"article","og_title":"How to traverse an array in PHP and print? - DevOps | SRE | DevSecOps","og_description":"The most common task with arrays is to do something with every element\u2014for instance, sending mail to each element of an array of addresses, updating each file...","og_url":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/","og_site_name":"DevOps | SRE | DevSecOps","article_published_time":"2022-11-03T12:17:41+00:00","article_modified_time":"2022-11-04T05:55:58+00:00","og_image":[{"url":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-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-to-traverse-an-array-in-php-and-print\/#article","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/"},"author":{"name":"rahulkr kr","@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"headline":"How to traverse an array in PHP and print?","datePublished":"2022-11-03T12:17:41+00:00","dateModified":"2022-11-04T05:55:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/"},"wordCount":235,"commentCount":0,"image":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-1024x576.jpg","keywords":["Array in PHP","echo statement","element of an array","foreach Construct","traverse an array in PHP","traverse arrays"],"articleSection":["Array","Echo Statement","Elements","PHP","Print","Traverse"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/","url":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/","name":"How to traverse an array in PHP and print? - DevOps | SRE | DevSecOps","isPartOf":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#primaryimage"},"image":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php-1024x576.jpg","datePublished":"2022-11-03T12:17:41+00:00","dateModified":"2022-11-04T05:55:58+00:00","author":{"@id":"https:\/\/www.devopstrainer.in\/blog\/#\/schema\/person\/e9fd1d88de76754aa189257cd197394d"},"breadcrumb":{"@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#primaryimage","url":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php.jpg","contentUrl":"https:\/\/www.devopstrainer.in\/blog\/wp-content\/uploads\/2022\/11\/traverse-array-in-php.jpg","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopstrainer.in\/blog\/how-to-traverse-an-array-in-php-and-print\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopstrainer.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How to traverse an array in PHP and print?"}]},{"@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\/542","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=542"}],"version-history":[{"count":2,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions\/551"}],"wp:attachment":[{"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopstrainer.in\/blog\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}