dongsui8162 2017-06-20 19:09
浏览 42
已采纳

每5个项目对数组中的项目进行分组,并在php中添加短划线

I'm building a loop that in my mind will output an array of items divided by a range of 5 numbers. I tried a few function like range, sort, implode, but I haven't found the right solution yet.

So far my code is the following:

foreach (range(0, 100, 5) as $number) {
  echo $number;
  print implode("-", str_split($number));
}

My goal is to output something like:

1-5
5-10
10-15

and so on that I can associate to anything, so essentially a range of numbers every a certain amount of numbers.

Plus I'm not sure if it's the right loop.

I guess I can obtain pretty much the same result with a for loop like:

for($i = 0; $i < 100; $i+=5)
{
     implode("-", str_split($i));
}

Where's my mistake?

UPDATE I probably forget to mention that everything is in a select item:

<select class="drops" name="largesan">
    <option selected value> -- How Many Sandwiches? -- </option><?php
    foreach (range(0, 100, 5) as $numbers)
    {
        $mynumber = $numbers . '-' . $numbers + 5;
        ?>
        <option value="<?php echo $numbers;?>"><?php echo $mynumber;?></option>
        <?php
    }
    ?>
</select>
  • 写回答

2条回答 默认 最新

  • doutongxuan1614 2017-06-20 19:13
    关注

    You could use a foreach of this type

      foreach (range(0, 90, 5) as $number) {
        $myStr = $number . '-'. $number + 5 ;
        echo '<option value="'. $myStr .'">'. $myStr.'</option>' ;
    
      }
    

    This is the code you should use ..

    select class="drops" name="largesan"> 
      <option selected value> -- How Many Sandwiches? -- </option>
        <?php 
            foreach (range(0, 90, 5) as $number) { 
    
                  $myStr = $number . '-'. $number + 5 ; 
                  echo '<option value="'.  $myStr. '">'. $myStr .'</option>' ;
            }
    
         ?> 
     </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名