dongpou7275 2014-02-20 02:00
浏览 102
已采纳

PHP如何创建增加数字的数组?

as the title says i am trying to create an increasing numbers array for example:

I have a number 30 and i want to create an array out of it like

$numbers = array(6,12,18,24,30);
// then extract data from mysql
foreach( $numbers as $LIMITNUMBER ){
   $query = "SELECT * FROM table WHERE id=id ORDER BY ASC LIMIT 0,".$LIMITNUMBER;
}

The 30 number above could be any number 100 or 200 but it always divides by 6 so the array first value has to be 6 and then +6 addition to the previous value.

  • 写回答

1条回答 默认 最新

  • duanbei1903 2014-02-20 02:09
    关注

    The easiest way to do this would be to use a function implementing PHP 'range'.

    Here's an example based on your question:

    function incrementToMax($max) {
        foreach (range(6, $max, 6) as $currentMax) {
           $query = "SELECT * FROM table WHERE id=id ORDER BY ASC LIMIT 0,".$currentMax;
        }
    }
    

    Example usage:

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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示