Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php foreach仅在数组的一个值上循环_Php_Foreach - Fatal编程技术网

php foreach仅在数组的一个值上循环

php foreach仅在数组的一个值上循环,php,foreach,Php,Foreach,这是我的密码: $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 4

这是我的密码:

$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 10green 11green 12green 13green 14green 15green 16green 17blue 18blue 19blue 20blue 21blue 22blue 23blue 24yellow 25yellow 26yellow 27yellow 28yellow 29yellow 30yellow 31

我应该在代码中更改什么?

您可以使用数组上的foreach循环和$nom从1到$bag3的for循环来简化代码:


您可以使用数组上的foreach循环和$nom从1到$bag3的for循环来简化代码:


这是一个邪恶/狡猾的循环,尼克。我很好奇,它是否总是以整数结尾,但之后没有重复的文本?正如您可能已经注意到的,数组和我并不是最好的朋友。@FunkFortyNiner输出中的换行可能更清楚,但是的,它总是以一个整型的$row值结束。这是一个邪恶/棘手的循环。我很好奇,它是否总是以整数结尾,但之后没有重复的文本?正如您可能已经注意到的,数组和我并不是最好的朋友。@FunkFortyNiner输出中的换行可能更清楚,但是是的,它总是以一个整型的$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
$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