döngü çalışmaz için de dizi değer katacak, neden?

2 Cevap

Bir göz atın:

for ($i=1; $i<=$nr_of_pics; $i++) {
    	${'image_src' . $i} = $image_id.'_'.$i;
    	$img_array = imageSize(${'image_src' . $i}, $i, $category);


    }

ve buradan (basitleştirilmiş) ImageSize fonksiyonudur:

$myarr = array();
    $myarr['thumb_image_' . $nr . '_width'] = $thumb_width;
    $myarr['thumb_image_' . $nr . '_height'] = $thumb_height;
    $myarr['image_' . $nr . '_width'] = $width;
    $myarr['image_' . $nr . '_height'] = $height;
    return $myarr;

The function gets called and OWERWRITES the value of the $img_array inside the foor-loop, but I need it to 'increment' and add values after another in every loop. So basically, it replaces its value every time it loops. What should I do here?

Teşekkürler

2 Cevap

Sen $img_array her zaman döngü üzerine yazıyorsanız. Değiştirmeyi deneyin

$img_array = imageSize(${'image_src' . $i}, $i, $category);

karşı

$img_array[] = imageSize(${'image_src' . $i}, $i, $category);

This will add an element karşı $img_array instead of replacing its value.

İlk kod bölümünde yeni bir dizi oluşturun sonra yerine onun değerini ayar ImageSize bir parametre olarak iletin:

$img_array = array();
for ($i=1; $i<=$nr_of_pics; $i++) {
    ${'image_src' . $i} = $image_id.'_'.$i;
    imageSize(${'image_src' . $i}, $i, $category, $img_array);
}

ImageSize yöntemde, sadece $ img_array yeni değerler eklemek, yeni bir dizi örneğini yoktur.