我做了以下'foreach',我需要循环10次然后停止。我不能使用for命令。
<?php
$x=array("Seb","Ginna","Shane","Guy","Jackie","Frances","John","Alec","Jon","Sam","Chris","Paula");
foreach ($x as $value)
{
echo $value . ",";
}
?>
我使用的是这个:
<?php
$theNames = array('Seb', 'Ginna', 'Shane', 'Guy', 'Jackie', 'Frances', 'John', 'Alec', 'Jon', 'Sam', 'Chris', 'Paula');
$toOutput = implode(",", $theNames);
for ($i=0; $i < 10; $i++) {
print $toOutput."<br/>";
}
?>
以前的代码按照我想要的方式工作但是我需要它在foreach循环中工作
答案 0 :(得分:9)
for
?我没有看到for
。
foreach (range(1, 10) as $i) {
foreach ($names as $name) {
echo $name . ', ';
}
echo '<br />';
}
$names = array("Seb", "Ginna", "Shane", "Guy", "Jackie", "Frances", "John", "Alec", "Jon", "Sam", "Chris", "Paula");
foreach (range(1, 10) as $i) {
foreach ($names as $name) {
echo $name . ', ';
}
echo '<br />' . PHP_EOL;
}
现在输出:
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
Seb, Ginna, Shane, Guy, Jackie, Frances, John, Alec, Jon, Sam, Chris, Paula, <br />
答案 1 :(得分:5)
有关。每。它遍历数组中的每个对象。你可以解决这个问题并像for循环一样计算,但你现在应该只使用for。
答案 2 :(得分:3)
此代码应该这样做:
<?php
$i = 0;
$x = array("Seb","Ginna","Shane","Guy","Jackie","Frances","John","Alec","Jon","Sam","Joe","Chris","Paula");
foreach ($x as $value)
{
if ($i++ > 9) break;
echo $value . ",";
}
答案 3 :(得分:2)
您可以使用PHP range()
创建一个包含10个项目的数组,然后使用foreach循环,如下所示
$counter = range(1, 10);
foreach($counter as $v)
echo implode(',', $x) . '<br />';
implode()
使用给定的 glue 连接所有数组元素
答案 4 :(得分:0)
我想你只想要一个slice of the array。
$theNames = array('Seb', 'Ginna', 'Shane', 'Guy', 'Jackie', 'Frances', 'John', 'Alec', 'Jon', 'Sam', 'Chris', 'Paula');
$names = array_slice($theNames, 0, 10);
print implode(",", $names) . "\n<br>";
答案 5 :(得分:0)
试试这个,它打印出你阵列的所有元素:
更新
$x=array("Seb","Ginna","Shane","Guy","Jackie","Frances","John","Alec","Jon","Sam","Chris","Paula");
$i=0;
foreach($x as $value){
if($i<10)
{
echo $value.', ';
$i++;
}
}
答案 6 :(得分:0)
$y=array("Seb","Ginna","Shane","Guy","Jackie","Frances","John","Alec","Jon","Sam","Chris","Paula");
for($x=0;$x<=10;$x++)
{
foreach ($y as $value)
{
echo $value . ",";
}
echo "<br>";
}
答案 7 :(得分:0)
这个怎么样
//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
}