PHP / Codeigniter - For Loop [未定义的偏移量]

时间:2012-03-29 13:43:05

标签: php codeigniter loops for-loop

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&lt; = count($ status); $ i ++)时。我得到这个通知未定义的偏移量

2 个答案:

答案 0 :(得分:2)

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

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
}

答案 1 :(得分:0)

试试这个:

$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();

}