drmet46444 2014-07-22 16:36
浏览 31
已采纳

如何使用'for'和'count'加入多个'expr'

Hello all I am trying to find a solution for joining multiple 'expr' together using for with 'count'.Whatever combination I use does not work.

Currently I have:

for($i=0; $i<14; $i++) {

//echo some values for the table;

}

//echo a table;

}

This sets the amount of values to be used in my tables.

But I have a two breaks where the numbers are missing so I need something like:

e.g 1:

//this gives syntax error
for($i=0; $i<2; $i=4; $i<8; $i=10; $i<15; $i++) {

//echo some values for the table;

}

//echo a table;

}

e.g 2:

//just doesnt work
for($i=0; $i<2; $i++) for($i=4; $i<8; $i++) for($i=10; $i<15; $i++)  {

//echo some values for the table;

}

//echo a table;

}

e.g 3:

//breaks my table
for($i=0; $i<2; $i++);
for($i=4; $i<8; $i++);
for($i=10; $i<15; $i++)  {

//echo some values for the table;

}

//echo a table;

}

e.g 4:

//this one is straight out of the php manual & doesnt work
for($i=0; $i<2; $i++)  {
for($i=4; $i<8; $i++)  {
for($i=10; $i<15; $i++)  {

//echo some values for the table;

}
}
}
}

//echo a table;

}

Please can you help me I need to find a solution to this - many thanks.

  • 写回答

1条回答 默认 最新

  • ds3422222222 2014-07-22 16:49
    关注

    Iterate over the numbers you expect by forming an array of them with array_merge() and range():

    // $i=0; $i<2; $i=4; $i<8; $i=10; $i<15
    foreach( array_merge( range( 0, 2), range( 4, 8), range( 10, 15)) as $i) {
        echo $i;
    }
    

    Output:

    0 1 2 4 5 6 7 8 10 11 12 13 14 15
    

    An alternative approach, you can form the range from 0 - 15, then remove 3 and 9 and iterate (Here the values we are interested in are in the key of the array, and not the value):

    $arr = array_flip(range( 0, 15));
    unset($arr[3]);
    unset($arr[9]);
    foreach( $arr as $i => $garbage) {
        echo $i . "
    ";
    }
    

    It produces the same output as above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码