PHP for循环停止太早

时间:2015-06-26 08:33:48

标签: php

我有一个循环,它会增加网站上显示的内容限制(json内容)。

问题是循环在50处停止。当我在浏览器中调用它们时,使用增量生成的链接正常工作。显示内容。

但我只存储了前50个的内容,然后停止了。

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 个答案:

答案 0 :(得分:0)

在这里,让我重新格式化您的代码,以便您可以看到与...相关的内容......

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;
}

您会注意到我在您的休息语句中添加了评论。 ;)