Welcome to WebmasterWorld Guest from 107.22.87.205

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

$array1 + $array2 with multi-dimensional arrays

     
4:59 pm on May 15, 2009 (gmt 0)

5+ Year Member



I have two similarly structured arrays and i want to take all the values from the second array and put them into the first array in the appropriate place (AND overwrite the original value if it was set).

$array1 + $array2 doesn't seem to like multi-dimensional arrays. array_merge didn't seem to be the answer either because there could be numeric keys involved. I thought about writing a recursive function to loop through all the values, but i couldn't figure out how to find the appropriate place in the other array to put the new value.

it seems like there is a fairly good lack of built in recursive functions for arrays.

any ideas?

6:48 pm on May 15, 2009 (gmt 0)

5+ Year Member



Could be as simple as writing a loop of some sort. Can you show us the arrays?
2:05 pm on May 18, 2009 (gmt 0)

5+ Year Member



$arr1[key][test][1] = '1' ;
$arr1[key][test][2][a] = '2a' ;
$arr1[key][test][2][b] = '2b' ;
$arr1[key][test][2][c] = '2c' ;
$arr1[key][test][2][d][i] = '2di' ;
$arr1[key][test][2][d][ii] = '2dii' ;
$arr1[key][test][3] = '3' ;
$arr1[key][test][4] = '4' ;

$arr2[test][1] = '1-overwritten' ;
$arr2[test][2][a] = '2a-overwritten' ;
$arr2[test][2][d][ii] = '2dii-overwritten' ;

and i want it to result in:

$arr3[key][test][1] = '1-overwritten' ;
$arr3[key][test][2][a] = '2a-overwritten' ;
$arr3[key][test][2][b] = '2b' ;
$arr3[key][test][2][c] = '2c' ;
$arr3[key][test][2][d][i] = '2di' ;
$arr3[key][test][2][d][ii] = '2dii-overwritten' ;
$arr3[key][test][3] = '3' ;
$arr3[key][test][4] = '4' ;

9:17 pm on May 18, 2009 (gmt 0)

5+ Year Member



Hm.. this is a bit more complicated that I anticipated lol...
I am running php 4 so I haven't been able to play with all the new php array functions. If you are in the same boat, likely you'll have to write a function. array_merge($arr1, $arr2) returns $arr2. Perhaps if you have php 5 you can do array_merge() -> array_combine(). Sorry, I can't be of any help - looks like this is one tough egg to crack! Good luck.
11:08 pm on May 18, 2009 (gmt 0)

5+ Year Member



[php.net...]

Might have the answer you are looking for.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month