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

為に。各。配列内のすべてのオブジェクトをループします。これを回避して for ループのようにカウントすることはできますが、この時点では for を使用する必要があります。

于 2013-05-01T15:26:13.510 に答える
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 に答える
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

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>";
于 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 に答える