{"id":1004,"date":"2019-02-24T15:29:59","date_gmt":"2019-02-24T15:29:59","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=1004"},"modified":"2020-05-31T14:05:29","modified_gmt":"2020-05-31T14:05:29","slug":"php-loop-through-array","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/","title":{"rendered":"PHP Loop through array"},"content":{"rendered":"<p>There are some methods to look through one array in PHP. The functions are foreach, while and for.\u00a0 In this article, we will show examples on PHP loop through array using each of these functions. By these examples, there will be a loop in the PHP script, and on each member of the array, the PHP script will show its value.<\/p>\n<p>Instead, any operation can be performed for each member of the array found.<\/p>\n<h3>PHP Loop through array using foreach()<\/h3>\n<p>The foreach\u00a0function is one of the most used for looping in PHP. As the name says: &#8220;For each&#8221; member, do such tasks.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nforeach ($array as $key)\r\n{\r\n    echo \"Value: $key &lt;br&gt;\";\r\n}\r\n\r\n?&gt;<\/pre>\n<h3>Loop through\u00a0a multidimensional array in PHP<\/h3>\n<p>The command foreach can be also used to look inside multidimensional <a href=\"https:\/\/www.copahost.com\/blog\/php-array-to-string\/\" >arrays in PHP<\/a>. Its usage will depend on the structure of the array.<\/p>\n<p>Example:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n\/\/ Let's build the array structure\r\n$array[1]['name'] = \"John\";\r\n$array[1]['phone'] = \"+1 888 9567834\";\r\n$array[2]['name'] = \"Doe\";\r\n$array[2]['phone'] = \"+44 32 5600 673\";\r\n$array[3]['name'] = \"Robert\";\r\n$array[3]['phone'] = \"+1 45 5634 0843\";\r\n$array[4] = \"Maria\";\r\n\r\n\r\nforeach ($array as $key1 =&gt; $value)\r\n{\r\n    echo \"&lt;br&gt; Value: $key1 Value: $value&lt;br&gt;\";\r\n    \r\n    \/\/ If it's an array, let's do another look inside it\r\n    if (is_array($value))\r\n    {\r\n        foreach ($value as $key2)\r\n        {\r\n            echo \"---- $key2 &lt;br&gt;\";\r\n        }\r\n    }\r\n\r\n    \/\/ if it's a simple string (non-array), let's print it\r\n    else\r\n    {\r\n        echo \"---- $value\";\r\n    }\r\n}\r\n\r\n\r\n?&gt;<\/pre>\n<p>So, the output:<\/p>\n<pre class=\"lang:default decode:true \">Value: 1 Value: Array\r\n---- John\u00a0\r\n---- +1 888 9567834\u00a0\r\n\r\nValue: 2 Value: Array\r\n---- Doe\u00a0\r\n---- +44 32 5600 673\u00a0\r\n\r\nValue: 3 Value: Array\r\n---- Robert\u00a0\r\n---- +1 45 5634 0843\u00a0\r\n\r\nValue: 4 Value: Maria\r\n---- Maria<\/pre>\n<p>In this example, $value on the first level of the array was brought as a new array. Then, you can run another foreach\u00a0on this new array, to get its contents. We have used the is_array() <a href=\"https:\/\/www.copahost.com\/blog\/check-update-php-version-cpanel\/\" >PHP function to check<\/a> whether it&#8217;s a simple string or an array.<\/p>\n<p>More information about the foreach() functon can be found <a href=\"http:\/\/php.net\/manual\/en\/control-structures.foreach.php\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are some methods to look through one array in PHP. The functions are foreach, while and for.\u00a0 In this article, we will show examples on PHP loop through array using each of these functions. By these examples, there will be a loop in the PHP script, and on each member of the array, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[133],"tags":[],"class_list":["post-1004","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Loop through array - Copahost<\/title>\n<meta name=\"description\" content=\"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.\" \/>\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.copahost.com\/blog\/php-loop-through-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Loop through array - Copahost\" \/>\n<meta property=\"og:description\" content=\"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-24T15:29:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-31T14:05:29+00:00\" \/>\n<meta name=\"author\" content=\"Gustavo Gallas\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gustavo Gallas\" \/>\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.copahost.com\/blog\/php-loop-through-array\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\"},\"author\":{\"name\":\"Gustavo Gallas\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246\"},\"headline\":\"PHP Loop through array\",\"datePublished\":\"2019-02-24T15:29:59+00:00\",\"dateModified\":\"2020-05-31T14:05:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\"},\"wordCount\":203,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\",\"name\":\"PHP Loop through array - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"datePublished\":\"2019-02-24T15:29:59+00:00\",\"dateModified\":\"2020-05-31T14:05:29+00:00\",\"description\":\"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Loop through array\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246\",\"name\":\"Gustavo Gallas\",\"description\":\"Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development\u200b. Also have brewing skills.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/gustavo-gallas-107926196\/\"],\"url\":\"https:\/\/www.copahost.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Loop through array - Copahost","description":"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.","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.copahost.com\/blog\/php-loop-through-array\/","og_locale":"en_US","og_type":"article","og_title":"PHP Loop through array - Copahost","og_description":"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.","og_url":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/","og_site_name":"Copahost","article_published_time":"2019-02-24T15:29:59+00:00","article_modified_time":"2020-05-31T14:05:29+00:00","author":"Gustavo Gallas","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gustavo Gallas","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/"},"author":{"name":"Gustavo Gallas","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246"},"headline":"PHP Loop through array","datePublished":"2019-02-24T15:29:59+00:00","dateModified":"2020-05-31T14:05:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/"},"wordCount":203,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/","url":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/","name":"PHP Loop through array - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"datePublished":"2019-02-24T15:29:59+00:00","dateModified":"2020-05-31T14:05:29+00:00","description":"Examples on PHP Loop through array. Sample scripts using foreach() function. Scripts in PHP to loop throught a simple array and a multidimensional.","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/php-loop-through-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/php-loop-through-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP Loop through array"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246","name":"Gustavo Gallas","description":"Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development\u200b. Also have brewing skills.","sameAs":["https:\/\/www.linkedin.com\/in\/gustavo-gallas-107926196\/"],"url":"https:\/\/www.copahost.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/1004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=1004"}],"version-history":[{"count":4,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":2458,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/1004\/revisions\/2458"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}