Php foreach循环语法

时间:2017-04-10 07:47:53

标签: php

尝试检查列表。需要一些foreach循环的帮助。似乎无法理解它的文档。

$(document).ready()

3 个答案:

答案 0 :(得分:1)

这不是正确的foreach语法。请改用for循环。

$a = array("marx","engels","lenin","stalin","mao zedong");
$a_size = count($a);
for($i=0; $i<$a_size; $i++) {
  echo $a[$i];
}

Foreach用法。

$comrades = array("marx","engels","lenin","stalin","mao zedong");
foreach($comrades as $comrade) {
  echo "Comrade ".$comrade;
}

答案 1 :(得分:0)

这不是foreach循环,这是一个带语法错误的for循环。这将是一个正确的FOR-Loop

foreach ($i = 0; $i <= $List; $i++) {
    foreach($a = 0; $a <= $List2; a++) { // You can't have a whitespace between "List and 2"
        // Do something inside inner loop
    }
    // Staff in outer loop      
}

http://php.net/manual/de/control-structures.for.php

如果你不是一个foreach循环:

foreach($your_array as $element) {
    // This loop is going to be executed for each element of your array. You can access the actual value by $element
}

http://php.net/manual/de/control-structures.foreach.php

答案 2 :(得分:0)

$i = 0 // defines starting value
$i <= $List // condition (while true loop goes on)
$i++ // change of the loop variable that at some point will end the loop
这个循环里面的

是嵌套循环 我想在嵌套循环中它应该是“$ List”2而不是“$ List 2”