亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php foreach 只是在數組的一個值上循環

php foreach 只是在數組的一個值上循環

PHP
慕無忌1623718 2022-05-27 16:28:37
這是我的代碼:$bag3 = 7;$row = 4;$nom = 1;$arr = array("red", "green", "blue", "yellow");while ($bag3){while ($nom <= $bag3){    echo $ay." ".$row;    $nom++;    $row++;}if ($nom == $bag3){    $nom = 1;}}這是輸出:red 4red 5red 6red 7red 8red 9red 10 我希望它遍歷所有數組值:red、green、blue和yellow. 像這樣:red 4red 5red 6red 7red 8red 9red 10green 11green 12green 13green 14green 15green 16green 17blue 18blue 19blue 20blue 21blue 22blue 23blue 24yellow 25yellow 26yellow 27yellow 28yellow 29yellow 30yellow 31我應該在我的代碼中更改什么?
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

foreach您可以通過數組上的for循環和$nom從 1 到的循環來簡化代碼$bag3:


$bag3 = 7;

$row = 4;

$arr = array("red", "green", "blue", "yellow");

foreach ($arr as $ay) {

    for ($nom = 1; $nom <= $bag3; $nom++, $row++){

        echo $ay." ".$row;

    }

}

輸出:


red 4red 5red 6red 7red 8red 9red 10green 11green 12green 13green 14green 15green 16green 17blue 18blue 19blue 20blue 21blue 22blue 23blue 24yellow 25yellow 26yellow 27yellow 28yellow 29yellow 30yellow 31


查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號