{"id":130,"date":"2007-09-15T09:30:43","date_gmt":"2007-09-15T01:30:43","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=130"},"modified":"2010-01-17T19:16:23","modified_gmt":"2010-01-17T11:16:23","slug":"phpforeach","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=130","title":{"rendered":"[PHP]foreach"},"content":{"rendered":"<p><strong>\u5167\u5bb9\u4f86\u6e90\uff1a<a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/control-structures.foreach.html\" target=\"_blank\">LINK&nbsp;<\/a>&nbsp;&nbsp;&nbsp; (\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0)<a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/control-structures.foreach.html\" target=\"_blank\"><br \/><\/a><\/strong><\/p>\n<p>     PHP 4\uff08\u4e0d\u662f PHP 3\uff09\u5305\u62ec\u4e86 <tt class=\"literal\">foreach<\/tt> \u7d50\u69cb\uff0c\u548c Perl \u4ee5\u53ca\u5176\u4ed6\u8a9e\u8a00\u5f88\u50cf\u3002\u9019\u53ea\u662f\u4e00\u7a2e\u904d\u6b77\u9663\u5217\u7c21\u4fbf\u65b9\u6cd5\u3002<tt class=\"literal\">foreach<\/tt> \u50c5\u80fd\u7528\u65bc\u9663\u5217\uff0c\u7576\u8a66\u5716\u5c07\u5176\u7528\u65bc\u5176\u5b83\u8cc7\u6599\u578b\u5225\u6216\u8005\u4e00\u500b\u672a\u521d\u59cb\u5316\u7684\u8b8a\u6578\u6642\u6703\u7522\u751f\u932f\u8aa4\u3002\u6709\u5169\u7a2e\u8a9e\u6cd5\uff0c\u7b2c\u4e8c\u7a2e\u6bd4\u8f03\u6b21\u8981\u4f46\u537b\u662f\u7b2c\u4e00\u7a2e\u7684\u6709\u7528\u7684\u64f4\u5c55\u3002     <\/p>\n<div class=\"informalexample\"><a name=\"AEN5079\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"programlisting\">foreach(array_expression as $value) statement<br \/>foreach(array_expression as $key =&gt; $value) statement<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>     \u7b2c\u4e00\u7a2e\u683c\u5f0f\u904d\u6b77\u7d66\u5b9a\u7684 <tt class=\"literal\">array_expression<\/tt> \u9663\u5217\u3002\u6bcf\u6b21\u5faa\u74b0\u4e2d\uff0c\u7576\u524d\u55ae\u5143\u7684\u503c\u88ab\u8ce6\u7d66 <tt class=\"literal\">$value<\/tt> \u4e26\u4e14\u9663\u5217\u5167\u90e8\u7684\u6307\u91dd\u5411\u524d\u79fb\u4e00\u6b65\uff08\u56e0\u6b64\u4e0b\u4e00\u6b21\u5faa\u74b0\u4e2d\u5c07\u6703\u5f97\u5230\u4e0b\u4e00\u500b\u55ae\u5143\uff09\u3002    <\/p>\n<p>     \u7b2c\u4e8c\u7a2e\u683c\u5f0f\u505a\u540c\u6a23\u7684\u4e8b\uff0c\u53ea\u9664\u4e86\u7576\u524d\u55ae\u5143\u7684\u9375\u503c\u4e5f\u6703\u5728\u6bcf\u6b21\u5faa\u74b0\u4e2d\u88ab\u8ce6\u7d66\u8b8a\u6578 <tt class=\"literal\">$key<\/tt>\u3002    <\/p>\n<p>&nbsp;<\/p>\n<div class=\"note\">\n<blockquote class=\"note\">\n<p><strong>\u8a3b: <\/strong>       \u7576 <tt class=\"literal\">foreach<\/tt> \u958b\u59cb\u57f7\u884c\u6642\uff0c\u9663\u5217\u5167\u90e8\u7684\u6307\u91dd\u6703\u81ea\u52d5\u6307\u5411\u7b2c\u4e00\u500b\u55ae\u5143\u3002\u9019\u610f\u5473\u8457\u4e0d\u9700\u8981\u5728 <tt class=\"literal\">foreach<\/tt> \u5faa\u74b0\u4e4b\u524d\u547c\u53eb <a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/function.reset.html\"><strong class=\"function\">reset()<\/strong><\/a>\u3002      <\/p>\n<\/blockquote>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"note\">\n<blockquote class=\"note\">\n<p><strong>\u8a3b: <\/strong>       \u6b64\u5916\u6ce8\u610f <tt class=\"literal\">foreach<\/tt> \u6240\u64cd\u4f5c\u7684\u662f\u6307\u5b9a\u9663\u5217\u7684\u4e00\u500b\u62f7\u8c9d\uff0c\u800c\u4e0d\u662f\u8a72\u9663\u5217\u672c\u8eab\u3002\u56e0\u6b64\u5373\u4f7f\u6709 <a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/function.each.html\"><strong class=\"function\">each()<\/strong><\/a> \u7684\u5efa\u69cb\uff0c\u539f\u9663\u5217\u6307\u91dd\u4e5f\u6c92\u6709\u8b8a\uff0c\u9663\u5217\u55ae\u5143\u7684\u503c\u4e5f\u4e0d\u53d7\u5f71\u97ff\u3002       <\/p>\n<\/blockquote>\n<\/div>\n<div class=\"note\">\n<blockquote class=\"note\">\n<p><strong>\u8a3b: <\/strong>      <tt class=\"literal\">foreach<\/tt> \u4e0d\u652f\u63f4\u7528&ldquo;@&rdquo;\u4f86\u7981\u6b62\u932f\u8aa4\u8a0a\u606f\u7684\u80fd\u529b\u3002     <\/p>\n<\/blockquote>\n<\/div>\n<p>     \u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86\u4ee5\u4e0b\u7684\u4ee3\u78bc\u529f\u80fd\u5b8c\u5168\u76f8\u540c\uff1a     <\/p>\n<div class=\"informalexample\"><a name=\"AEN5101\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">reset ($arr);<br \/>while (list(, $value) = each ($arr)) {<br \/>    echo &quot;Value: $value&lt;br&gt;n&quot;;<br \/>}<br \/><br \/>foreach ($arr as $value) {<br \/>    echo &quot;Value: $value&lt;br&gt;n&quot;;<br \/>}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>     \u4ee5\u4e0b\u4ee3\u78bc\u529f\u80fd\u4e5f\u5b8c\u5168\u76f8\u540c\uff1a     <\/p>\n<div class=\"informalexample\"><a name=\"AEN5103\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">reset ($arr);<br \/>while (list($key, $value) = each ($arr)) {<br \/>    echo &quot;Key: $key; Value: $value&lt;br&gt;n&quot;;<br \/>}<br \/><br \/>foreach ($arr as $key =&gt; $value) {<br \/>    echo &quot;Key: $key; Value: $value&lt;br&gt;n&quot;;<br \/>}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>     \u793a\u7bc4\u7528\u6cd5\u7684\u66f4\u591a\u4f8b\u5b50\uff1a     <\/p>\n<div class=\"informalexample\"><a name=\"AEN5106\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">\/* foreach example 1: value only *\/<br \/><br \/>$a = array (1, 2, 3, 17);<br \/><br \/>foreach ($a as $v) {<br \/>   print &quot;Current value of $a: $v.n&quot;;<br \/>}<br \/><br \/>\/* foreach example 2: value (with key printed for illustration) *\/<br \/><br \/>$a = array (1, 2, 3, 17);<br \/><br \/>$i = 0; \/* for illustrative purposes only *\/<br \/><br \/>foreach($a as $v) {<br \/>    print &quot;$a[$i] =&gt; $v.n&quot;;<br \/>    $i++;<br \/>}<br \/><br \/>\/* foreach example 3: key and value *\/<br \/><br \/>$a = array (<br \/>    &quot;one&quot; =&gt; 1,<br \/>    &quot;two&quot; =&gt; 2,<br \/>    &quot;three&quot; =&gt; 3,<br \/>    &quot;seventeen&quot; =&gt; 17<br \/>);<br \/><br \/>foreach($a as $k =&gt; $v) {<br \/>    print &quot;$a[$k] =&gt; $v.n&quot;;<br \/>}<br \/><br \/>\/* foreach example 4: multi-dimensional arrays *\/<br \/><br \/>$a[0][0] = &quot;a&quot;;<br \/>$a[0][1] = &quot;b&quot;;<br \/>$a[1][0] = &quot;y&quot;;<br \/>$a[1][1] = &quot;z&quot;;<br \/><br \/>foreach($a as $v1) {<br \/>    foreach ($v1 as $v2) {<br \/>        print &quot;$v2n&quot;;<br \/>    }<br \/>}<br \/><br \/>\/* foreach example 5: dynamic arrays *\/<br \/><br \/>foreach(array(1, 2, 3, 4, 5) as $v) {<br \/>    print &quot;$vn&quot;;<br \/>}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5167\u5bb9\u4f86\u6e90\uff1aLINK&nbsp;&nbsp;&nbsp;&nbsp; (\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=130\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}