{"id":744,"date":"2025-08-01T15:27:30","date_gmt":"2025-08-01T06:27:30","guid":{"rendered":"https:\/\/nexive.tech\/?p=744"},"modified":"2025-08-01T15:27:31","modified_gmt":"2025-08-01T06:27:31","slug":"%e3%80%90%e3%82%b5%e3%82%af%e3%83%83%e3%81%a8%e8%a7%a3%e8%aa%ac%e3%80%91php%e3%81%aefor%e3%83%bbwhile%e3%83%bbforeach%e3%81%ae%e9%81%95%e3%81%84%e3%81%a8%e4%bd%bf%e3%81%84%e5%88%86%e3%81%91","status":"publish","type":"post","link":"https:\/\/nexive.tech\/?p=744","title":{"rendered":"\u3010\u30b5\u30af\u30c3\u3068\u89e3\u8aac\u3011PHP\u306efor\u30fbwhile\u30fbforeach\u306e\u9055\u3044\u3068\u4f7f\u3044\u5206\u3051"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP\u306b\u304a\u3051\u308b<code>for<\/code>\u3001<code>while<\/code>\u3001<code>foreach<\/code>\u306e\u57fa\u672c\u7684\u306a\u69cb\u6587\u3068\u9055\u3044\u3092\u89e3\u8aac<\/li>\n\n\n\n<li>\u5404\u30eb\u30fc\u30d7\u306e\u4ee3\u8868\u7684\u306a\u7528\u9014\u3068\u9078\u5b9a\u57fa\u6e96\u3092\u5b9f\u884c\u4f8b\u4ed8\u304d\u3067\u7d39\u4ecb<\/li>\n\n\n\n<li>\u5b9f\u52d9\u3067\u3042\u308a\u304c\u3061\u306a\u8aa4\u7528\u3084\u975e\u52b9\u7387\u306a\u4f7f\u3044\u65b9\u306b\u3082\u6ce8\u610f\u559a\u8d77<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP\u306efor\u30fbwhile\u30fbforeach\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>PHP\u306b\u304a\u3044\u3066\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u3092\u884c\u3046\u4ee3\u8868\u7684\u306a\u69cb\u6587\u306b\u306f\u3001<code>for<\/code>\u3001<code>while<\/code>\u3001<code>foreach<\/code>\u306e3\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u306e\u69cb\u6587\u306f\u5236\u5fa1\u65b9\u5f0f\u3084\u5bfe\u8c61\u30c7\u30fc\u30bf\u306e\u7a2e\u985e\u306b\u5fdc\u3058\u3066\u4f7f\u3044\u5206\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p><code>for<\/code>\u3068<code>while<\/code>\u306f\u30ab\u30a6\u30f3\u30bf\u5f0f\u306e\u30eb\u30fc\u30d7\u3067\u3001\u6570\u5024\u3084\u6761\u4ef6\u306b\u57fa\u3065\u304f\u53cd\u5fa9\u51e6\u7406\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u65b9\u3001<code>foreach<\/code>\u306f\u914d\u5217\u3084\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u8981\u7d20\u3092\u9806\u756a\u306b\u51e6\u7406\u3059\u308b\u305f\u3081\u306b\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001\u9023\u60f3\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u64cd\u4f5c\u306b\u3082\u4fbf\u5229\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u52d9\u3067\u306f\u3001\u51e6\u7406\u5bfe\u8c61\u306e\u30c7\u30fc\u30bf\u578b\u3084\u30a4\u30c6\u30ec\u30fc\u30c8\u6761\u4ef6\u306b\u5fdc\u3058\u3066\u6700\u9069\u306a\u30eb\u30fc\u30d7\u69cb\u6587\u3092\u9078\u5b9a\u3059\u308b\u3053\u3068\u304c\u3001\u53ef\u8aad\u6027\u3084\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u4e0a\u3067\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8a73\u7d30\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u5024\u3067\u5236\u5fa1\u3059\u308b\u7e70\u308a\u8fd4\u3057\u51e6\u7406\uff1afor\u6587<\/h3>\n\n\n\n<p>\u8981\u7d20\u6570\u304c\u660e\u78ba\u306a\u5834\u5408\u3084\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u5236\u5fa1\u304c\u5fc5\u8981\u306a\u5834\u9762\u3067\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ 0\u304b\u30894\u307e\u3067\u306e\u6570\u5024\u3092\u51fa\u529b\nfor ($i = 0; $i &lt; 5; $i++) {\n    echo $i . \", \";\n}\n\/\/ \u51fa\u529b: 0, 1, 2, 3, 4,\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ 0\u304b\u30894\u307e\u3067\u306e\u6570\u5024\u3092\u51fa\u529b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\"> &lt; <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\">++) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$i<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;, &quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u51fa\u529b: 0, 1, 2, 3, 4,<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><code>for (\u521d\u671f\u5316; \u6761\u4ef6; \u66f4\u65b0)<\/code>\u3068\u3044\u3046\u69cb\u6587\u3092\u53d6\u308a\u3001\u6761\u4ef6\u304c\u771f\u306e\u9593\u30eb\u30fc\u30d7\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002\u30ab\u30a6\u30f3\u30bf\u5909\u6570\u306e\u5236\u5fa1\u304c\u660e\u793a\u7684\u3067\u3042\u308b\u305f\u3081\u3001\u30eb\u30fc\u30d7\u306e\u56de\u6570\u3092\u7d30\u304b\u304f\u8abf\u6574\u3057\u3084\u3059\u3044\u306e\u304c\u7279\u5fb4\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6761\u4ef6\u3092\u6e80\u305f\u3059\u9593\u30eb\u30fc\u30d7\u3059\u308b\uff1awhile\u6587<\/h3>\n\n\n\n<p>\u7e70\u308a\u8fd4\u3057\u6761\u4ef6\u304c\u4e8b\u524d\u306b\u78ba\u5b9a\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u3084\u3001\u5165\u529b\u5024\u306a\u3069\u3067\u30eb\u30fc\u30d7\u7d42\u4e86\u3092\u5224\u65ad\u3059\u308b\u5834\u9762\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ 5\u672a\u6e80\u306e\u9593\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\n$count = 0;\nwhile ($count &lt; 5) {\n    echo $count . \", \";\n    $count++;\n}\n\/\/ \u51fa\u529b: 0, 1, 2, 3, 4,\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ 5\u672a\u6e80\u306e\u9593\u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\"> &lt; <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;, &quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$count<\/span><span style=\"color: #D4D4D4\">++;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u51fa\u529b: 0, 1, 2, 3, 4,<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><code>while (\u6761\u4ef6)<\/code>\u306b\u3088\u308a\u3001\u6761\u4ef6\u304c\u507d\u306b\u306a\u308b\u307e\u3067\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002\u30eb\u30fc\u30d7\u4e2d\u3067\u306e\u5909\u6570\u66f4\u65b0\u3092\u5fd8\u308c\u308b\u3068\u7121\u9650\u30eb\u30fc\u30d7\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6271\u3046\uff1aforeach\u6587<\/h3>\n\n\n\n<p>\u914d\u5217\u3084\u30a4\u30c6\u30e9\u30d6\u30eb\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30921\u8981\u7d20\u305a\u3064\u51e6\u7406\u3059\u308b\u969b\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ \u914d\u5217\u306e\u8981\u7d20\u3092\u51fa\u529b\n$fruits = &#91;\"apple\", \"banana\", \"cherry\"&#93;;\nforeach ($fruits as $fruit) {\n    echo $fruit . \", \";\n}\n\/\/ \u51fa\u529b: apple, banana, cherry,\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u914d\u5217\u306e\u8981\u7d20\u3092\u51fa\u529b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$fruits<\/span><span style=\"color: #D4D4D4\"> = &#91;<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;banana&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;cherry&quot;<\/span><span style=\"color: #D4D4D4\">&#93;;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$fruits<\/span><span style=\"color: #D4D4D4\"> as <\/span><span style=\"color: #9CDCFE\">$fruit<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$fruit<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;, &quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u51fa\u529b: apple, banana, cherry,<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><code>foreach ($\u914d\u5217 as $\u8981\u7d20)<\/code>\u306e\u5f62\u5f0f\u3067\u8a18\u8ff0\u3057\u3001\u30eb\u30fc\u30d7\u5185\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u610f\u8b58\u305b\u305a\u306b\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002\u9023\u60f3\u914d\u5217\u306e\u5834\u5408\u306f\u30ad\u30fc\u3068\u5024\u3092\u540c\u6642\u306b\u53d6\u5f97\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>$prices = &#91;\"apple\" => 100, \"banana\" => 80&#93;;\nforeach ($prices as $name => $price) {\n    echo \"$name: $price\\n\";\n}\n\/\/ \u51fa\u529b:\n\/\/ apple: 100\n\/\/ banana: 80\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9CDCFE\">$prices<\/span><span style=\"color: #D4D4D4\"> = &#91;<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\"> =&gt; <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&quot;banana&quot;<\/span><span style=\"color: #D4D4D4\"> =&gt; <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\">&#93;;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$prices<\/span><span style=\"color: #D4D4D4\"> as <\/span><span style=\"color: #9CDCFE\">$name<\/span><span style=\"color: #D4D4D4\"> =&gt; <\/span><span style=\"color: #9CDCFE\">$price<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #9CDCFE\">$name<\/span><span style=\"color: #CE9178\">: <\/span><span style=\"color: #9CDCFE\">$price<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u51fa\u529b:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ apple: 100<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ banana: 80<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">for\u3068foreach\u306e\u9078\u5b9a\u57fa\u6e96<\/h3>\n\n\n\n<p>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5fc5\u8981\u306a\u51e6\u7406\uff08\u4f4d\u7f6e\u306b\u3088\u308b\u6761\u4ef6\u5206\u5c90\u3084\u5076\u5947\u306e\u5224\u5b9a\u306a\u3069\uff09\u306f<code>for<\/code>\u304c\u5411\u3044\u3066\u3044\u307e\u3059\u3002\u5bfe\u3057\u3066\u3001\u5358\u306b\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u51e6\u7406\u3059\u308b\u3060\u3051\u3067\u3042\u308c\u3070<code>foreach<\/code>\u304c\u7c21\u6f54\u3067\u5b89\u5168\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001<code>foreach<\/code>\u306f\u5185\u90e8\u30dd\u30a4\u30f3\u30bf\u3092\u81ea\u52d5\u7ba1\u7406\u3059\u308b\u305f\u3081\u3001\u30a4\u30c6\u30ec\u30fc\u30bf\u306e\u72b6\u614b\u3092\u660e\u793a\u7684\u306b\u6271\u3046\u5fc5\u8981\u304c\u306a\u3044\u3068\u3044\u3046\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u3088\u304f\u3042\u308b\u30df\u30b9\u30fb\u8aa4\u89e3\u30fb\u843d\u3068\u3057\u7a74<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7121\u9650\u30eb\u30fc\u30d7\u306e\u767a\u751f<\/h3>\n\n\n\n<p><code>while<\/code>\u6587\u3084<code>for<\/code>\u6587\u3067\u30eb\u30fc\u30d7\u5909\u6570\u306e\u66f4\u65b0\u3092\u5fd8\u308c\u308b\u3068\u7121\u9650\u30eb\u30fc\u30d7\u306b\u306a\u308a\u307e\u3059\u3002\u7279\u306b<code>while<\/code>\u6587\u3067\u306f\u6761\u4ef6\u5f0f\u306e\u66f4\u65b0\u3092\u30eb\u30fc\u30d7\u5916\u3067\u8a18\u8ff0\u3057\u3066\u3057\u307e\u3046\u30df\u30b9\u304c\u591a\u304f\u898b\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">foreach\u306e\u53c2\u7167\u843d\u3068\u3057<\/h3>\n\n\n\n<p>\u914d\u5217\u306e\u8981\u7d20\u3092\u66f8\u304d\u63db\u3048\u308b\u306b\u306f\u3001\u53c2\u7167\u6e21\u3057\uff08<code>foreach ($arr as &amp;$val)<\/code>\uff09\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u5fd8\u308c\u308b\u3068\u3001\u30eb\u30fc\u30d7\u5185\u306e\u5909\u66f4\u304c\u5143\u306e\u914d\u5217\u306b\u53cd\u6620\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>$nums = &#91;1, 2, 3&#93;;\nforeach ($nums as &amp;$n) {\n    $n *= 2;\n}\nprint_r($nums);\n\/\/ \u51fa\u529b: Array ( &#91;0&#93; => 2 &#91;1&#93; => 4 &#91;2&#93; => 6 )\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9CDCFE\">$nums<\/span><span style=\"color: #D4D4D4\"> = &#91;<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">&#93;;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$nums<\/span><span style=\"color: #D4D4D4\"> as <\/span><span style=\"color: #569CD6\">&amp;<\/span><span style=\"color: #9CDCFE\">$n<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$n<\/span><span style=\"color: #D4D4D4\"> *= <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print_r<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">$nums<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u51fa\u529b: Array ( &#91;0&#93; =&gt; 2 &#91;1&#93; =&gt; 4 &#91;2&#93; =&gt; 6 )<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">foreach\u5f8c\u306e\u53c2\u7167\u30d0\u30b0<\/h3>\n\n\n\n<p>\u53c2\u7167\u3092\u4f7f\u3063\u305f<code>foreach<\/code>\u306e\u5f8c\u306f\u3001\u30eb\u30fc\u30d7\u5909\u6570\u3092\u660e\u793a\u7684\u306b<code>unset()<\/code>\u3059\u308b\u7656\u3092\u3064\u3051\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u3092\u6020\u308b\u3068\u3001\u4e88\u671f\u305b\u306c\u53c2\u7167\u5148\u306e\u5909\u66f4\u304c\u4ed6\u306e\u51e6\u7406\u306b\u5f71\u97ff\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>unset($n); \/\/ \u5fc5\u8981\u306a\u5f8c\u51e6\u7406\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #DCDCAA\">unset<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">$n<\/span><span style=\"color: #D4D4D4\">); <\/span><span style=\"color: #6A9955\">\/\/ \u5fc5\u8981\u306a\u5f8c\u51e6\u7406<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u307e\u3068\u3081<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>for<\/code>\u306f\u6570\u5024\u5236\u5fa1\u306b\u5f37\u304f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5fc5\u8981\u306a\u5834\u5408\u306b\u6709\u52b9<\/li>\n\n\n\n<li><code>while<\/code>\u306f\u4e8b\u524d\u6761\u4ef6\u304c\u672a\u77e5\u306e\u53cd\u5fa9\u51e6\u7406\u306b\u5411\u304f<\/li>\n\n\n\n<li><code>foreach<\/code>\u306f\u914d\u5217\u64cd\u4f5c\u306b\u7279\u5316\u3057\u3001\u30b3\u30fc\u30c9\u306e\u7c21\u6f54\u5316\u306b\u5bc4\u4e0e<\/li>\n\n\n\n<li>\u69cb\u6587\u3054\u3068\u306e\u7279\u6027\u3068\u843d\u3068\u3057\u7a74\u3092\u7406\u89e3\u3057\u3001\u4f7f\u3044\u5206\u3051\u308b\u3053\u3068\u304c\u91cd\u8981<\/li>\n\n\n\n<li>\u7279\u306b\u53c2\u7167\u51e6\u7406\u3084\u7121\u9650\u30eb\u30fc\u30d7\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8 2. PHP\u306efor\u30fbwhile\u30fbforeach\u3068\u306f\uff1f PHP\u306b\u304a\u3044\u3066\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u3092\u884c\u3046\u4ee3\u8868\u7684\u306a\u69cb\u6587\u306b\u306f\u3001for\u3001while\u3001foreach\u306e3\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u306e\u69cb\u6587\u306f\u5236\u5fa1\u65b9\u5f0f\u3084\u5bfe\u8c61\u30c7\u30fc\u30bf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":448,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=744"}],"version-history":[{"count":1,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions\/745"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/media\/448"}],"wp:attachment":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}