4

我做了以下“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 循环中工作

4

8 回答 8

17

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 />
于 2013-05-01T15:32:40.403 回答
5

这段代码应该这样做:

<?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 . ",";
}
于 2013-05-01T15:27:14.583 回答
5

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

于 2013-05-01T15:26:13.510 回答
2

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

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

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

于 2013-05-01T15:26:47.553 回答
1

这个怎么样

//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
}
于 2013-05-01T15:56:34.960 回答
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++; 
 }
}
于 2013-05-01T15:40:41.597 回答
0

我认为您只想要数组的一部分

$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>";
于 2013-05-01T15:29:11.373 回答
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>";
}
于 2013-05-01T15:50:24.267 回答