dongzhuifeng1843 2015-06-26 08:33
浏览 36

PHP for循环停止太早

I have a loop which increments the limit of the content which is showing on a website (json content).

The problem is that the loop stops at 50. The links that are generated with the incrementation are working fine when I'm calling them in the browser. The content is shown.

But I store only the content of the first 50 and then it stops.

public static function getAllCustomers() {
    $rest = Rest::getInstance();
    $spaces = self::getSpaces();
    $customers = array();
    $path = SpacePath::buildPath();
    for ($i = 0;
    ; $i += Paths::$MAX_CONTENT_LENGTH) {
        $temp = $path . '?start=' . $i . '&limit=50';
        $content = Helper::toArray($temp);
        print_r($temp);
// print_r(empty($content['results']));
        if (!empty($content['results'])) {
            foreach ($content ['results'] as $space) {
                if (!preg_match('|^(.*?)-([0-9]+)|i', $space ['name'], $matches)) {
                    continue;
                }
                $customer = (object) array(
                            'name' => $matches [0],
                            'ident' => $matches [1],
                            'id' => $matches [2],
                            'space_key' => $space ['key'],
                            'options' => array()
                );
                $customers [] = $customer;
            }
            break;
        }
    }
    print_r($customers);
    return $customers;
}
  • 写回答

1条回答 默认 最新

  • dpdjv9559 2015-07-24 10:38
    关注

    Here, let me reformat your code so you can see what relates to what...

    public static function getAllCustomers() 
    {
      $rest = Rest::getInstance();
      $spaces = self::getSpaces();
      $customers = array();
      $path = SpacePath::buildPath();
    
      for ($i = 0; ; $i += Paths::$MAX_CONTENT_LENGTH) 
      {
        $temp = $path . '?start=' . $i . '&limit=50';
        $content = Helper::toArray($temp);
        print_r($temp);
        //print_r(empty($content['results']));
    
        if (!empty($content['results'])) 
        {
          foreach ($content ['results'] as $space) 
          {
            if (!preg_match('|^(.*?)-([0-9]+)|i', $space ['name'], $matches)) 
            {
              continue;
            }
            $customer = (object) array('name' => $matches [0]
                                      ,'ident' => $matches [1]
                                      ,'id' => $matches [2]
                                      ,'space_key' => $space ['key']
                                      ,'options' => array()
                                      );
    
            $customers [] = $customer;
          }       
          break; // oops
        }
      }
      print_r($customers);
      return $customers;
    }
    

    You'll notice I added a comment to your break statement. ;)

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像