【问题标题】:PHP/Codeigniter - For Loop [Undefined offset]PHP/Codeigniter - For 循环 [未定义的偏移量]
【发布时间】:2012-03-29 13:43:05
【问题描述】:
for($i=0;$i<count($status);$i++)
{
    $conf = array(
        'source_image' => $status[$i]['full_path'],
        'new_image' => $this->upload_path . '/thumbs',
        'maintain_ratio' => true,
        'width' => 200,
        'height' => 200
    );

    $this->load->library('image_lib', $conf);
    $this->image_lib->resize();

    $this->image_lib->clear(); // complete reset    
    $this->image_lib->initialize($conf); // complete reset
}

            .

总是错过最后一个缩略图创建周期。尝试 for($i=0;$i 时。我收到此通知未定义的偏移量

【问题讨论】:

  • 当我保持条件小于 $status 时我错过了图像,当我保持条件小于等于时,脚本会创建一个空白数组。
  • 你能把数组的var_dump贴出来好吗?
  • 你确定full_path索引存在于$status的最后一项吗?
  • 请不要在循环条件下使用count()(上面加糖)。在循环之前将数组长度缓存在一个变量中。

标签: php codeigniter loops for-loop


【解决方案1】:

通过使用for 循环,您假设数组的键是连续的,但它们可能不是。您还假设每个二级数组都有一个 full_path 键,但它可能没有。请改用foreach,并对full_path 键进行isset() 检查:

foreach ($status as $item)
{

    if (!isset($item['full_path'])) continue;

    $conf = array(
        'source_image' => $item['full_path'],
        'new_image' => $this->upload_path . '/thumbs',
        'maintain_ratio' => true,
        'width' => 200,
        'height' => 200
    );

    $this->load->library('image_lib', $conf);
    $this->image_lib->resize();

    $this->image_lib->clear(); // complete reset    
    $this->image_lib->initialize($conf); // complete reset
}

【讨论】:

    【解决方案2】:

    试试这个:

    $this->load->library('image_lib');
    $stat = array_values($status);
    for($i=0;$i<count($stat);$i++)
    {
    $conf = array(
        'source_image' => $stat[$i]['full_path'],
        'new_image' => $this->upload_path . '/thumbs',
        'maintain_ratio' => true,
        'width' => 200,
        'height' => 200
    );
    
    $this->image_lib->initialize($conf); // complete reset
    $this->image_lib->resize();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多