tags:

views:

31

answers:

2

I want the array ($img_array) to increase every time it's looped, but it wont. The length of the array is 0 when I echo it 'echo count(array);'

function imageSize($name, $nr, $category, $myarr){
    $path = 'ad_images/'.$category.'/'.$name.'.jpg';
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
    list($width, $height) = getimagesize($path);
    list($thumb_width, $thumb_height) = getimagesize($path_thumb);
    //$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;

}

    if ($nr_of_pics!=0){
     $image_id = end( explode( '_', $ad_id ) );
     $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);
     }
    }
    echo count($img_array);
    echo $img_array['image_3_width'];

What is wrong?

ALSO, when I echo the $img_array element as you can see, it can't find it, says undefined index... BUT when I echo the element from INSIDE the function, it will work!

If you need more input let me know and I will update this Q.

Thanks

+2  A: 

Change this line

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

to

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

you are returning an array in your function but then not doing anything with it so it's gone.

Marek Karbarz
Another option would be to pass the array by reference (This would be faster and require less memory).imageSize(${'image_src' . $i}, $i, $category,
Zoomzoom83
A: 

imageSize returns an array, which i guess should be the accumulated 'image array', so set the $img_array to the result of the function:

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