473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

arrays don't merge

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.  

1 1679
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

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

Similar topics

2
by: nickdu | last post by:
Is there a tool that will merge XML documents? We also need the reverse, we need to be able to create a Diff of two documents. What we're trying to do is just store differences of documents at...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
2
by: ad | last post by:
I have two array like string a1= {"dog","dock","deer"} string a2= {"lion","tiger"} How can I merge a1 and a2 to a3 a3: {"dog","dock","deer", "lion","tiger"}
13
by: ralphedge | last post by:
These sorts work fine on 100000 ints but if I go much higher they will both segmentation fault **************************MERGESORT********************* mergesort(int *a, int size) //a is...
5
by: John | last post by:
Hi Is there a way to merge two or more single dimension string arrays into a single, single dimension string array? Thanks Regards
6
by: amitabh.mehra | last post by:
I want to trap errors (RI errors), if any, that might turn up during merge in DB2. Is there some feature like the one in Oracle: MERGE INTO... WHEN MATCHED THEN UPDATE... WHEN NOT MATCHED THEN...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
7
by: mooon33358 | last post by:
I have a little problem with implementing a recursive merge sort I have to use a function mergesort that takes 3 arguments - an array, its size, and an help array(i.e mergesort(int array, int...
3
by: sscanf | last post by:
Hello, I have a empty oldDataSet. Then with the proper adapter i Fill a new DataSet newDataSet with two tables (Table1 and Table2) that dont exist in oldDataSet. Then i do...
6
by: p4willi | last post by:
I've defined two arrays in a Module called PubVars Public Type LinesRec CORRECT As Integer QUESTION As String PROC As Integer PAY As Integer End Type Public Lines_Array() As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.