{"id":416,"date":"2025-06-10T20:07:58","date_gmt":"2025-06-10T12:07:58","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/416.html"},"modified":"2025-06-10T20:07:58","modified_gmt":"2025-06-10T12:07:58","slug":"%e5%9c%a8php%e4%b8%ad%e6%8e%8c%e6%8f%a1foreach%e5%be%aa%e7%8e%af","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/416.html","title":{"rendered":"\u5728PHP\u4e2d\u638c\u63e1foreach\u5faa\u73af"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;foreach &#24490;&#29615;&#26159;&#19968;&#31181;&#24378;&#22823;&#30340;&#36845;&#20195;&#25968;&#32452;&#21644;&#23545;&#35937;&#30340;&#26041;&#27861;&#65292;&#29702;&#35299;&#20854;&#32454;&#33410;&#21487;&#20197;&#26174;&#33879;&#25552;&#39640;&#32534;&#31243;&#24037;&#20316;&#27969;&#31243;&#30340;&#25928;&#29575;&#12290;&#35753;&#25105;&#20204;&#28145;&#20837;&#25506;&#35752; foreach &#30340;&#32454;&#33410;&#65292;&#24182;&#36890;&#36807;&#23454;&#38469;&#31034;&#20363;&#36827;&#34892;&#35828;&#26126;&#12290;<\/p>\n<h2>&#22522;&#26412;&#29992;&#27861;<\/h2>\n<p>&#22312;&#26680;&#24515;&#65292;foreach &#24490;&#29615;&#29992;&#20110;&#36941;&#21382;&#25968;&#32452;&#20013;&#30340;&#27599;&#20010;&#20803;&#32032;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#31034;&#20363;&#65306;<\/p>\n<pre><code>&lt;?php\n$colors = array('red', 'green', 'blue');\nforeach ($colors as $color) {\n    echo $color . 'n';\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#23558;&#36755;&#20986;&#25968;&#32452;&#20013;&#30340;&#27599;&#20010;&#39068;&#33394;&#65306;&#32418;&#33394;&#12289;&#32511;&#33394;&#21644;&#34013;&#33394;&#12290;<\/p>\n<h2>&#20851;&#32852;&#25968;&#32452;<\/h2>\n<p>&#8216;foreach&#8217; &#20063;&#25797;&#38271;&#22788;&#29702;&#20851;&#32852;&#25968;&#32452;&#65292;&#20854;&#20013;&#38190;&#21487;&#20197;&#36890;&#36807;&#20197;&#19979;&#26041;&#24335;&#35775;&#38382;&#65306;<\/p>\n<pre><code>&lt;?php\n$favorites = array('food' =&gt; 'pizza', 'color' =&gt; 'blue', 'movie' =&gt; 'The Matrix');\nforeach ($favorites as $key =&gt; $value) {\n    echo 'My favorite ' . $key . ' is ' . $value . 'n';\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#20010;&#24490;&#29615;&#20250;&#36755;&#20986;&#65306;&#25105;&#26368;&#21916;&#27426;&#30340;&#39135;&#21697;&#26159;&#25259;&#33832; &#25105;&#26368;&#21916;&#27426;&#30340;&#39068;&#33394;&#26159;&#34013;&#33394; &#25105;&#26368;&#21916;&#27426;&#30340;&#30005;&#24433;&#26159;&#12298;&#40657;&#23458;&#24093;&#22269;&#12299;<\/p>\n<h2>&#20462;&#25913;&#25968;&#32452;&#20803;&#32032;<\/h2>\n<p>&#22914;&#26524;&#20320;&#38656;&#35201;&#22312;&#36941;&#21382;&#36807;&#31243;&#20013;&#20462;&#25913;&#20803;&#32032;&#65292;&#21487;&#20197;&#20351;&#29992;&lsquo;foreach&rsquo;&#24490;&#29615;&#20013;&#30340;&#24341;&#29992;&#65306;<\/p>\n<pre><code>&lt;?php\n$numbers = [1, 2, 3, 4];\nforeach ($numbers as &amp;$number) {\n    $number *= 2;\n}\nunset($number); \/\/ Break the reference with the last element\n?&gt;<\/code><\/pre>\n<p>&#22312;&#36825;&#19968;&#36718;&#20043;&#21518;&#65292;&#21464;&#37327;&lsquo;$numbers&rsquo;&#23558;&#21253;&#21547;&#20381;&#27425;&#20026;2&#12289;4&#12289;6&#21644;8&#30340;&#25968;&#32452;&#12290;<\/p>\n<h2>&#36845;&#20195;&#23545;&#35937;<\/h2>\n<p>PHP&#30340;foreach&#19981;&#20165;&#21487;&#20197;&#36845;&#20195;&#23545;&#35937;&#23646;&#24615;&#65292;&#24403;&#26377;&#19968;&#20010;&#21253;&#21547;&#23646;&#24615;&#30340;&#23545;&#35937;&#26102;&#65292;&#21487;&#20197;&#20351;&#29992;foreach&#26469;&#35775;&#38382;&#27599;&#20010;&#23646;&#24615;&#65306;<\/p>\n<pre><code>&lt;?php\nclass Car {\n    public $make = 'Toyota';\n    public $model = 'Corolla';\n}\n$car = new Car();\nforeach ($car as $key =&gt; $value) {\n    echo $key . ': ' . $value . 'n';\n}\n?&gt;<\/code><\/pre>\n<p>&#35813;&#21629;&#20196;&#30340;&#36755;&#20986;&#24212;&#20026;&#65306;make: Toyota model: Corolla<\/p>\n<h2>&#25511;&#21046;&#24490;&#29615;&#25191;&#34892;<\/h2>\n<p>foreach &#24490;&#29615;&#21487;&#20197;&#36890;&#36807;&#20351;&#29992; break &#35821;&#21477;&#25552;&#21069;&#36864;&#20986;&#24490;&#29615;&#65292;&#25110;&#32773;&#20351;&#29992; continue &#35821;&#21477;&#36339;&#36807;&#24403;&#21069;&#36845;&#20195;&#65292;&#36716;&#32780;&#36827;&#34892;&#19979;&#19968;&#27425;&#36845;&#20195;&#65306;<\/p>\n<pre><code>&lt;?php\ndate_default_timezone_set($timezone_identifier);\n$scores = array('Player1' =&gt; 300, 'Player2' =&gt; 560, 'Player3' =&gt; 120);\nforeach ($scores as $player =&gt; $score) {\n    if ($score &gt; 500) {\n        break; \/\/ Exit the loop if a player has more than 500 points\n    }\n    echo $player . ' has ' . $score . ' pointsn';\n}\n?&gt;<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#24403;&#24490;&#29615;&#36935;&#21040;&#19968;&#20010;&#20998;&#25968;&#22823;&#20110;500&#30340;&#29609;&#23478;&#26102;&#65292;&#23427;&#23601;&#20250;&#32456;&#27490;&#12290;<\/p>\n<h2>&#39640;&#32423;&#27010;&#24565;<\/h2>\n<p>&#22312;&#22788;&#29702;&#22797;&#26434;&#30340;&#25968;&#25454;&#32467;&#26500;&#25110;&#20248;&#21270;&#24615;&#33021;&#26102;&#65292;&#20250;&#29992;&#21040;&#26356;&#39640;&#32423;&#30340;&ldquo;foreach&rdquo;&#25216;&#26415;&#65292;&#20363;&#22914;&#20351;&#29992;&ldquo;list()&rdquo;&#20989;&#25968;&#26469;&#23558;&#23884;&#22871;&#25968;&#32452;&#20013;&#30340;&#20803;&#32032;&#36171;&#20540;&#32473;&#21464;&#37327;&#65306;<\/p>\n<pre><code>&lt;?php\n$colors = array(\n    array('red', 'FF0000'),\n    array('green', '00FF00'),\n    array('blue', '0000FF')\n);\nforeach ($colors as list($color, $hex)) {\n    echo $color . ' has the hex code ' . $hex . 'n';\n}\n?&gt;<\/code><\/pre>\n<p>&#35813;&#20195;&#30721;&#20250;&#28165;&#26224;&#22320;&#36755;&#20986;&#39068;&#33394;&#21450;&#20854;&#23545;&#24212;&#30340;&#21313;&#20845;&#36827;&#21046;&#20195;&#30721;&#12290;<\/p>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#22312;&#38381;&#24149;&#26102;&#65292;foreach &#24490;&#29615;&#26159;PHP&#20013;&#30340;&#19968;&#20010;&#22522;&#26412;&#20294;&#33267;&#20851;&#37325;&#35201;&#30340;&#26500;&#36896;&#12290;&#36890;&#36807;&#26126;&#26234;&#30340;&#24212;&#29992;&#21644;&#32451;&#20064;&#65292;&#23427;&#21487;&#20197;&#25104;&#20026;&#26377;&#25928;&#25968;&#32452;&#21644;&#23545;&#35937;&#25805;&#20316;&#30340;&#26530;&#32445;&#12290;&#25506;&#32034;&#12289;&#23454;&#39564;&#24182;&#25484;&#25569; foreach &#20197;&#25552;&#21319;&#24744;&#30340; PHP &#32534;&#31243;&#33021;&#21147;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#22312;PHP&#20013;&#65292;foreach &#24490;&#29615;&#26159;&#19968;&#31181;&#24378;&#22823;&#30340;&#36845;&#20195;&#25968;&#32452;&#21644;&#038;#23..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-416","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=416"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=416"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=416"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}