tags:

views:

19

answers:

2

Take a look:

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


    }

and here is the imageSize function (simplified):

$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?

Thanks

+3  A: 

You're overwriting $img_array each time through the loop. Try changing

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

to

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

This will add an element to $img_array instead of replacing its value.

Asaph
it wont work, the array isn't increased... hmm
Camran
@camran: Try adding `$img_array = array();` before your main `for` loop.
Asaph
I have tried that, I have posted another Q regarding this: http://stackoverflow.com/questions/1794716/cant-get-this-array-working-new-to-arrays-please-some-help
Camran
+1  A: 

Create a new array in the first code section, then pass it as a parameter to imageSize instead of setting its value:

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

In the imageSize method, don't instantiate a new array, just add new values to $img_array.

Kaleb Brasee