带范围的foreach不起作用 [英] foreach with range does not work

查看:81
本文介绍了带范围的foreach不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

   function my_function()
{
$states = array('schwarz', 'rot', 'blau');
foreach(range(0, 5) as $number) {
  foreach ($states as $state) {
    $result = "<img src=\"inventory_images/8.jpg\" onclick=\"changecolor(this)\" name=\"nummer.$number\" />";
    $testPath = "transactions/Ordner$number/$state.png";
    if (file_exists($testPath)) {
      $result = $testPath;
    }
  }
  break;
}
return $result;
}
$imagesPerLine = array(1=>1, 2=>2); $default = 3;
$lines = array(1, 2, 3);
$html="";
foreach ($lines as $line) {
if (!isset($imagesPerLine[$line])) {
  $imagesPerLine[$line] = $default;
}
$html.= "<tr>\n";
for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html.=sprintf("<td>%s</td>\n", my_function());
}
$html.="</tr>\n";

}
echo $html;

这是输出:

<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>

现在我不知道如何配置我的foreach部分以获取名称值= n + 1.输出应为:

now i dont know how to configure my foreach part to get the name-value = n+1.the output should be:

<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.0" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.1" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.2" /></td>
</tr>
<tr>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.3" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.4" /></td>
<td><img src="inventory_images/8.jpg" onclick="changecolor(this)" name="nummer.5" /></td>
</tr>

首先,我想到了休息".或返回$ result;"被放置错了,但我现在不这么认为. 有人可以帮助我更改脚本吗? 谢谢和问候!

firstly i thought, "break;" or "return $result;" is placed wrong, but i dont think so now. could anybody help me to change my script maybe?? thanks and greetings!!

推荐答案

您的逻辑有缺陷.因为您从函数中返回了字符串,并在外部打印出来,所以您需要更改操作方式.当前,您正在执行以下操作:

Your logic is flawed. Because you return the string from your function, and print it outside, you need to change how you are doing it. Currently, you're doing this:

for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html.=sprintf("<td>%s</td>\n", my_function());
}

对我来说听起来很像,您需要使函数用<td>...</td>包装其内容,然后进行连接.因此,在您的函数内应该执行以下操作:

Sounds to me like you need to make your function wrap its content with <td>...</td>, and also concatenate. So inside your function you should do:

$result .= "<td>etc etc $number etc etc</td>\n";

$result = "";放在该函数的顶部也是一个好习惯.

It would also be good practice to put $result = ""; at the top of that function.

现在,当您返回$result时,应该将所有内容放到这些循环中,然后通过更改调用循环将它们添加到$html中:

Now, when you return $result, you should get all the stuff you put together inside those loops, and you add that to $html by changing your calling loop:

for ($i = 1; $i <= $imagesPerLine[$line]; $i++) {
  $html .= my_function();
}

这篇关于带范围的foreach不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆