php 如何让 Foreach 循环重复 10 次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16321068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:55:27  来源:igfitidea点击:

How can i get a Foreach loop to repeat 10 times?

phpforeach

提问by user1839483

I have made the following 'foreach' which i need to make loop 10 times then stop. I cannot use a for command.

我做了以下“foreach”,我需要循环 10 次然后停止。我不能使用 for 命令。

<?php
$x=array("Seb","Ginna","Shane","Guy","Hymanie","Frances","John","Alec","Jon","Sam","Chris","Paula");
foreach ($x as $value)
{
echo $value . ",";
}
?>

Prvious to this i used:

在此之前我使用过:

<?php
$theNames = array('Seb', 'Ginna', 'Shane', 'Guy', 'Hymanie', 'Frances', 'John', 'Alec', 'Jon', 'Sam', 'Chris', 'Paula');

$toOutput = implode(",", $theNames);

for ($i=0; $i < 10; $i++) { 
print $toOutput."<br/>";
}
?>

The previous code works the way i want it to however i need it to work in a foreach loop

前面的代码按我希望的方式工作,但是我需要它在 foreach 循环中工作

回答by Dan Lugg

for? I don't see a for.

for? 我没有看到for.

foreach (range(1, 10) as $i) {
    foreach ($names as $name) {
        echo $name . ', ';
    }
    echo '<br />';
}


$names = array("Seb", "Ginna", "Shane", "Guy", "Hymanie", "Frances", "John", "Alec", "Jon", "Sam", "Chris", "Paula");
foreach (range(1, 10) as $i) {
    foreach ($names as $name) {
        echo $name . ', ';
    }
    echo '<br />' . PHP_EOL;
}

And now for the output:

现在输出:

Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Hymanie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />

回答by Pier-Luc Gendreau

For. Each. It loops through every object in the array. You can workaround that and count just like a for loop, but you should really just use a for at this point.

为了。每个。它遍历数组中的每个对象。您可以解决这个问题并像 for 循环一样计数,但此时您应该只使用 for。

回答by Mathieu Amiot

This code should do it:

这段代码应该这样做:

<?php

$i = 0;
$x = array("Seb","Ginna","Shane","Guy","Hymanie","Frances","John","Alec","Jon","Sam","Joe","Chris","Paula");
foreach ($x as $value)
{
    if ($i++ > 9) break;
    echo $value . ",";
}

回答by Ejaz

You could use PHP range()to create an array of 10 items then use foreach loop as follows

您可以使用 PHPrange()创建一个包含 10 个项目的数组,然后使用 foreach 循环,如下所示

  $counter = range(1, 10);
  foreach($counter as $v)
     echo implode(',', $x) . '<br />';

implode()joinsall array elements with given glue

implode()用给定的胶水连接所有数组元素

回答by user2267029

How about this

这个怎么样

//start your count at 1
$count = 1;

//start your loop
foreach ($array as $something) {

    //when your count is at 10 "continue" is to go to the end of the loop
    if ($count == 10) {
        continue;  
    }

    //this will add the next integer
    $count++;

    //end your loop
}

回答by r2d3

Try this, it prints out all element of your array:

试试这个,它会打印出数组的所有元素:

updated:

更新:

$x=array("Seb","Ginna","Shane","Guy","Hymanie","Frances","John","Alec","Jon","Sam??","Chris","Paula");
$i=0;
foreach($x as $value){
 if($i<10)
 { 
  echo $value.', ';
  $i++; 
 }
}

回答by raidenace

$y=array("Seb","Ginna","Shane","Guy","Hymanie","Frances","John","Alec","Jon","Sam","Chris","Paula");
for($x=0;$x<=10;$x++)
{
  foreach ($y as $value)
  {
    echo $value . ",";
  }
  echo "<br>";
}

回答by Xeoncross

I think you just want a slice of the array.

我想你只想要数组的一部分

$theNames = array('Seb', 'Ginna', 'Shane', 'Guy', 'Hymanie', 'Frances', 'John', 'Alec', 'Jon', 'Sam', 'Chris', 'Paula');

$names = array_slice($theNames, 0, 10);

print implode(",", $names) . "\n<br>";