PHP onay dizi elemanlarının ise 'tanımsız' vardır ve eğer öyleyse, (0) sıfır olarak bunları?

2 Cevap php

Ben böyle bir dizi oluşturma:

function imageSize($name, $nr, $category){
    $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;
}

I have hidden inputs on my page which values are taken from the array like this: (FIRST)

<input type="hidden" id="img1_width" value="<?php echo $img_array['image_1_width'];?>" />
        <input type="hidden" id="img1_height" value="<?php echo $img_array['image_1_height'];?>" />
            <input type="hidden" id="img1_th_width" value="<?php echo $img_array['thumb_image_1_width'];?>" />
                <input type="hidden" id="img1_th_height" value="<?php echo $img_array['thumb_image_1_height'];?>" />

(SECOND)

<input type="hidden" id="img2_width" value="<?php echo $img_array['image_2_width'];?>" />
        <input type="hidden" id="img2_height" value="<?php echo $img_array['image_2_height'];?>" />
            <input type="hidden" id="img2_th_width" value="<?php echo $img_array['thumb_image_2_width'];?>" />
                <input type="hidden" id="img2_th_height" value="<?php echo $img_array['thumb_image_2_height'];?>" />

Sadece 1 resim, daha sonra dizisi yalnızca KEZ adı verilecek ve ikinci görüntüyü çağırır girişler ('id' = 'img2' vb ile tüm girişler) varsa Şimdi, [(0)] {değerine sahip olacak }

My Q is this: Is there any way I can check the array length and set the remaining values all to '0' (zero) if they are not set?

Teşekkürler

2 Cevap

Ne onlar aracılığıyla size döngü eğer ...

<?php for ($i=0;$i<count($array);$i++): ?>

<input type="hidden" id="img{$i}_width" value="<?php echo $img_array["image_{$i}_width"];?>" />
<input type="hidden" id="img{$i}_height" value="<?php echo $img_array["image_{$i}_height"];?>" />
<input type="hidden" id="img{$i}_th_width" value="<?php echo $img_array["thumb_image_{$i}_width"];?>" />
<input type="hidden" id="img{$i}_th_height" value="<?php echo $img_array["thumb_image_{$i}_height"];?>" />

<?php endfor; ?>

Tanımsız eğer o 0 olacak böylece bir tamsayı değeri ayrıştırmak ...