从$ _FILES数组中删除空数组元素

时间:2011-10-31 12:03:20

标签: php array-merge

我有一个相册,在数据库中有一个“照片”字段,该字段具有序列化数据并且是64位编码的。我可以同时为相册上传最多21张照片(多次上传)。在编辑模式下尝试将新照片添加到相册时,我无法将新的$ _FILES数组与数据库中的旧数组合并。我想只更新我已更改的值。所以,假设我的相册中已有2张图片,我想添加第3张图片而不会丢失其他2张图片。我知道我需要使用unset()来删除空值,但我认为我没有正确地做到这一点。这是我的代码:

if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
        //select photo album and get photos and descriptions to be merged with new.
        $album_id           = $_REQUEST['album_id'];    
        $old_photos         = null;
        $old_descriptions   = null;

        $getAlbumQ = mysql_query("select * from albums where id='$album_id'");

        while ($old_album = mysql_fetch_array($getAlbumQ)) {
            $old_photos         = unserialize(base64_decode($old_album['photos']));
            $old_descriptions   = unserialize(base64_decode($old_album['descriptions']));
        }

        if (isset($_POST['album_name']) && isset($_POST['desc'])) {
            $name       = $_POST['album_name'];
            $desc       = $_POST['desc'];
            $idesc      = array();
            $target_path = "../uploads/albums/";



            foreach ($_FILES as $k => $v) {
                //first upload photos
                $path = $target_path . basename($v['name']); 
                if(move_uploaded_file($v['tmp_name'], $path)) {

                    $hasUpload = true;
                }   
            }



            for ($j = 1; $j < 21; $j++) {
                    $img_index  = $j;
                    $img_desc   = $_POST['desc_' . $img_index];
                    $asdf   = $_FILES['image_' . $img_index]['name'];

                    array_push($idesc, $img_desc);          

            }

            foreach( $_FILES['images']['name'] as $key => $value ) { 
                   if( empty($value) ) { 
                                       unset( $_FILES['images']['name'][$key] ); 
                    } 
            } 


            for ($i = 1; $i < 21; $i++) {


            if($_FILES['image_'.$i]['name']!= '')
            {
            $hasUpload = true;

            $presults       = array_merge($old_photos, $_FILES); //THE PROBLEM WITH MERGING ARRAYS OCCURS HERE
            $dresults       = array_merge($old_descriptions, $idesc);

            $images         = base64_encode(serialize($presults));
            $descriptions   = base64_encode(serialize($dresults));
            $posted         = date("Y-m-d H:i:s");
            }
            else {
                $hasUpload = false;

            $presults       = $old_photos;
            $dresults       = $idesc;

            $images         = base64_encode(serialize($presults));
            $descriptions   = base64_encode(serialize($dresults));
            $posted         = date("Y-m-d H:i:s");
            }
        }

        }
    }

2 个答案:

答案 0 :(得分:1)

我尝试了类似于Muu建议的东西。将它与在http://keithdevens.com/weblog/archive/2003/Mar/30/MergeTwoArrays找到的自定义合并函数的另一段代码结合起来,它运行良好!谢谢你的帮助

只需要在for($ i = 1; $ i&lt; 21; $ i ++)循环之外添加以下代码行:

//delete empty values for $_FILES array

foreach ($_FILES as $key => $value) {

                    foreach ($value as $k => $v) {
                         if ($k=='name' && $v!='') {
                             break;
                         } else {
                             unset($_FILES[$key]);
                         }
                   }
                 }

//custom array merge function 

function merge(&$a, &$b){
   $keys = array_keys($a);
   foreach($keys as $key){
       if(isset($b[$key])){
           if(is_array($a[$key]) and is_array($b[$key])){
               merge($a[$key],$b[$key]);
           }else{
               $a[$key] = $b[$key];
           }
       }
   }
   $keys = array_keys($b);
   foreach($keys as $key){
       if(!isset($a[$key])){
           $a[$key] = $b[$key];
       }
   }
}

然后代替array_merge(),我在for($ i = 1; $ i&lt; 21; $ i ++)循环中使用了merge():

 $presults       = merge($old_photos, $_FILES);

答案 1 :(得分:0)

为什么不忽略它们,而不是从数组中删除空值? 例如:

$nonEmptyArray = array();
foreach ($_FILES as $k => $v) {
    if (!$v) {
        continue;
    }
    $nonEmptyArray[$k] = $v;
}
$_FILES = $nonEmptyArray;