By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

arrays don't merge

P: 2
I have a photo album, with a 'photos' field in database which has serialized data and is base 64 encoded. I can upload up to 21 photos at the same time for the album (multiple uploading). When trying to add new photos to the album on edit mode, I can't get the new $_FILES array to merge with the old array in the database. I want to update only the values that I have changed. So let's say I already had 2 images inside my album, I would like to add a 3rd image without losing the other 2 images. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
  2.         //select photo album and get photos and descriptions to be merged with new.
  3.         $album_id           = $_REQUEST['album_id'];    
  4.         $old_photos         = null;
  5.         $old_descriptions   = null;
  6.  
  7.         $getAlbumQ = mysql_query("select * from albums where id='$album_id'");
  8.  
  9.         while ($old_album = mysql_fetch_array($getAlbumQ)) {
  10.             $old_photos         = unserialize(base64_decode($old_album['photos']));
  11.             $old_descriptions   = unserialize(base64_decode($old_album['descriptions']));
  12.         }
  13.  
  14.         if (isset($_POST['album_name']) && isset($_POST['desc'])) {
  15.             $name       = $_POST['album_name'];
  16.             $desc       = $_POST['desc'];
  17.             $idesc      = array();
  18.             $target_path = "../uploads/albums/";
  19.  
  20.  
  21.  
  22.             foreach ($_FILES as $k => $v) {
  23.                 //first upload photos
  24.                 $path = $target_path . basename($v['name']); 
  25.                 if(move_uploaded_file($v['tmp_name'], $path)) {
  26.  
  27.                     $hasUpload = true;
  28.                 }   
  29.             }
  30.  
  31.  
  32.  
  33.             for ($j = 1; $j < 21; $j++) {
  34.                     $img_index  = $j;
  35.                     $img_desc   = $_POST['desc_' . $img_index];
  36.  
  37.                     array_push($idesc, $img_desc);          
  38.  
  39.             }
  40.  
  41.            foreach ($_FILES as $key => $value) {
  42.  
  43.                         foreach ($value as $k => $v) {
  44.                              if ($k === 'name' && $v !== '') {
  45.                                  break;
  46.                              } else {
  47.                                  unset($_FILES[$key]);
  48.                              }
  49.                         }
  50.             }
  51.  
  52.  
  53.             for ($i = 1; $i < 21; $i++) {
  54.  
  55.  
  56.             if($_FILES['image_'.$i]['name']!= '')
  57.             {
  58.             $hasUpload = true;
  59.  
  60.             $presults       = array_merge($old_photos, $_FILES); //THE PROBLEM WITH MERGING ARRAYS OCCURS HERE
  61.             $dresults       = array_merge($old_descriptions, $idesc);
  62.  
  63.             $images         = base64_encode(serialize($presults));
  64.             $descriptions   = base64_encode(serialize($dresults));
  65.             $posted         = date("Y-m-d H:i:s");
  66.             }
  67.             else {
  68.                 $hasUpload = false;
  69.  
  70.             $presults       = $old_photos;
  71.             $dresults       = $idesc;
  72.  
  73.             $images         = base64_encode(serialize($presults));
  74.             $descriptions   = base64_encode(serialize($dresults));
  75.             $posted         = date("Y-m-d H:i:s");
  76.             }
  77.         }
  78.  
  79.         }
  80.     }
Nov 1 '11 #1

✓ answered by Kokos Koka

Just needed to add the following lines of code outside the for ($i = 1; $i < 21; $i++) loop:

Expand|Select|Wrap|Line Numbers
  1. //delete empty values for $_FILES array
  2.  
  3. foreach ($_FILES as $key => $value) {
  4.  
  5.                     foreach ($value as $k => $v) {
  6.                          if ($k=='name' && $v!='') {
  7.                              break;
  8.                          } else {
  9.                              unset($_FILES[$key]);
  10.                          }
  11.                    }
  12.                  }
  13.  
  14. //custom array merge function 
  15.  
  16. function merge(&$a, &$b){
  17.    $keys = array_keys($a);
  18.    foreach($keys as $key){
  19.        if(isset($b[$key])){
  20.            if(is_array($a[$key]) and is_array($b[$key])){
  21.                merge($a[$key],$b[$key]);
  22.            }else{
  23.                $a[$key] = $b[$key];
  24.            }
  25.        }
  26.    }
  27.    $keys = array_keys($b);
  28.    foreach($keys as $key){
  29.        if(!isset($a[$key])){
  30.            $a[$key] = $b[$key];
  31.        }
  32.    }
  33. }
  34.  
then instead of array_merge(), I used merge() inside the for ($i = 1; $i < 21; $i++) loop:

Expand|Select|Wrap|Line Numbers
  1. $presults       = merge($old_photos, $_FILES);
  2.  

Share this Question
Share on Google+
1 Reply


P: 2
Just needed to add the following lines of code outside the for ($i = 1; $i < 21; $i++) loop:

Expand|Select|Wrap|Line Numbers
  1. //delete empty values for $_FILES array
  2.  
  3. foreach ($_FILES as $key => $value) {
  4.  
  5.                     foreach ($value as $k => $v) {
  6.                          if ($k=='name' && $v!='') {
  7.                              break;
  8.                          } else {
  9.                              unset($_FILES[$key]);
  10.                          }
  11.                    }
  12.                  }
  13.  
  14. //custom array merge function 
  15.  
  16. function merge(&$a, &$b){
  17.    $keys = array_keys($a);
  18.    foreach($keys as $key){
  19.        if(isset($b[$key])){
  20.            if(is_array($a[$key]) and is_array($b[$key])){
  21.                merge($a[$key],$b[$key]);
  22.            }else{
  23.                $a[$key] = $b[$key];
  24.            }
  25.        }
  26.    }
  27.    $keys = array_keys($b);
  28.    foreach($keys as $key){
  29.        if(!isset($a[$key])){
  30.            $a[$key] = $b[$key];
  31.        }
  32.    }
  33. }
  34.  
then instead of array_merge(), I used merge() inside the for ($i = 1; $i < 21; $i++) loop:

Expand|Select|Wrap|Line Numbers
  1. $presults       = merge($old_photos, $_FILES);
  2.  
Nov 10 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.