{"id":69,"date":"2021-03-08T00:11:17","date_gmt":"2021-03-08T00:11:17","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=69"},"modified":"2025-04-06T03:04:31","modified_gmt":"2025-04-06T03:04:31","slug":"php-foreach","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-foreach\/","title":{"rendered":"PHP foreach"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use PHP <code>foreach<\/code> statement to loop over elements of an array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-foreach-statement'>Introduction to the PHP foreach statement <a href=\"#introduction-to-the-php-foreach-statement\" class=\"anchor\" id=\"introduction-to-the-php-foreach-statement\" title=\"Anchor for Introduction to the PHP foreach statement\">#<\/a><\/h2>\n\n\n\n<p>PHP provides you with the <code>foreach<\/code> statement that allows you to iterate over elements of an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a>, either an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">indexed array<\/a> or an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-associative-arrays\/\">associative array<\/a>.<\/p>\n\n\n\n<p>The <code>foreach<\/code> statement iterates over all elements in an array, one at a time. It starts with the first element and ends with the last one. Therefore, you don&#8217;t need to know the number of elements in an array upfront.<\/p>\n\n\n\n<p>The following flowchart illustrates how the <code>foreach<\/code> statement works:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"203\" height=\"368\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/php-foreach.png\" alt=\"PHP foreach\" class=\"wp-image-417\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-foreach.png 203w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-foreach-165x300.png 165w\" sizes=\"auto, (max-width: 203px) 100vw, 203px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='php-foreach-with-indexed-arrays'>PHP foreach with indexed arrays <a href=\"#php-foreach-with-indexed-arrays\" class=\"anchor\" id=\"php-foreach-with-indexed-arrays\" title=\"Anchor for PHP foreach with indexed arrays\">#<\/a><\/h2>\n\n\n\n<p>To iterate over all elements of an indexed array, you use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">foreach<\/span> ($array_name <span class=\"hljs-keyword\">as<\/span> $element) {\n    <span class=\"hljs-comment\">\/\/ process element here<\/span>\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When PHP encounters a <code>foreach<\/code> statement, it assigns the first element of the array to the variable following the <code>as<\/code> keyword (<code>$element<\/code>).<\/p>\n\n\n\n<p>In each iteration, PHP assigns the next array element to the <code>$element<\/code> variable. If PHP reaches the last element, the loop ends.<\/p>\n\n\n\n<p>The following example uses the <code>foreach<\/code> statement to display elements of the <code>$colors<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>];\n\n<span class=\"hljs-keyword\">foreach<\/span> ($colors <span class=\"hljs-keyword\">as<\/span> $color) {\n\t<span class=\"hljs-keyword\">echo<\/span> $color . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjb2xvcnMgPSBbJ3JlZCcsICdncmVlbicsICdibHVlJ107Cgpmb3JlYWNoICgkY29sb3JzIGFzICRjb2xvcikgewoJZWNobyAkY29sb3IgLiAnPGJyPic7Cn0\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">red\ngreen\nblue<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-foreach-with-an-associative-array'>PHP foreach with an associative array <a href=\"#php-foreach-with-an-associative-array\" class=\"anchor\" id=\"php-foreach-with-an-associative-array\" title=\"Anchor for PHP foreach with an associative array\">#<\/a><\/h2>\n\n\n\n<p>To iterate over elements of an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-associative-arrays\/\">associative array<\/a>, you use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">foreach<\/span> ($array_name <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value) {\n   <span class=\"hljs-comment\">\/\/process element here;<\/span>\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When PHP encounters the <code>foreach<\/code> statement, it accesses the first element and assigns:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The key of element to the <code>$key<\/code> variable.<\/li>\n\n\n\n<li>The value of the element to the <code>$value<\/code> variable.<\/li>\n<\/ul>\n\n\n\n<p>In each iteration, PHP assigns the key and value of the next element to the variables (<code>$key<\/code> and <code>$value<\/code>) that follows the <code>as<\/code> keyword. If the last element is reached, PHP ends the loop.<\/p>\n\n\n\n<p>The following example illustrates how to use the <code>foreach<\/code> statement to iterate over elements of the <code>captials<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$capitals = &#91;\n\t<span class=\"hljs-string\">'Japan'<\/span> =&gt; <span class=\"hljs-string\">'Tokyo'<\/span>,\n\t<span class=\"hljs-string\">'France'<\/span> =&gt; <span class=\"hljs-string\">'Paris'<\/span>,\n\t<span class=\"hljs-string\">'Germany'<\/span> =&gt; <span class=\"hljs-string\">'Berlin'<\/span>,\n\t<span class=\"hljs-string\">'United Kingdom'<\/span> =&gt; <span class=\"hljs-string\">'London'<\/span>,\n\t<span class=\"hljs-string\">'United States'<\/span> =&gt; <span class=\"hljs-string\">'Washington D.C.'<\/span>\n];\n\n<span class=\"hljs-keyword\">foreach<\/span> ($capitals <span class=\"hljs-keyword\">as<\/span> $country =&gt; $capital) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The capital city of $country is $capital\"<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjYXBpdGFscyA9IFsKCSdKYXBhbicgPT4gJ1Rva3lvJywKCSdGcmFuY2UnID0-ICdQYXJpcycsCgknR2VybWFueScgPT4gJ0JlcmxpbicsCgknVW5pdGVkIEtpbmdkb20nID0-ICdMb25kb24nLAoJJ1VuaXRlZCBTdGF0ZXMnID0-ICdXYXNoaW5ndG9uIEQuQy4nCl07Cgpmb3JlYWNoICgkY2FwaXRhbHMgYXMgJGNvdW50cnkgPT4gJGNhcGl0YWwpIHsKCWVjaG8gIlRoZSBjYXBpdGFsIGNpdHkgb2YgJGNvdW50cnkgaXMgJGNhcGl0YWwiIC4gJzxicj4nOwp9\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">The capital city of Japan is Tokyo\nThe capital city of France is Paris\nThe capital city of Germany is Berlin\nThe capital city of United Kingdom is London\nThe capital city of United States is Washington D.C.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>foreach($array_name as $element)<\/code> to iterate over elements of an indexed array.<\/li>\n\n\n\n<li>Use the <code>foreach($array_name as $key => $value)<\/code> to iterate over elements of an associative array.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"69\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-foreach\/\"\n\t\t\t\tdata-post-title=\"PHP foreach\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"69\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-foreach\/\"\n\t\t\t\tdata-post-title=\"PHP foreach\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use PHP foreach statement to loop over elements of an array.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":43,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-69","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/69\/revisions"}],"predecessor-version":[{"id":3046,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/69\/revisions\/3046"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}