Welcome to WebmasterWorld Guest from 54.146.217.179

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

# \$array1 + \$array2 with multi-dimensional arrays

#### noyearzero

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

#### Junior Member

joined:Jan 18, 2008
posts:99

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?

#### d40sithui

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

#### Preferred Member

joined:Jan 16, 2007
posts:477

Could be as simple as writing a loop of some sort. Can you show us the arrays?

#### noyearzero

2:05 pm on May 18, 2009 (gmt 0)

#### Junior Member

joined:Jan 18, 2008
posts:99

\$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' ;

#### d40sithui

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

#### Preferred Member

joined:Jan 16, 2007
posts:477

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.

#### acemaster

11:08 pm on May 18, 2009 (gmt 0)

#### New User

joined:Oct 25, 2008
posts:17

[php.net...]

Might have the answer you are looking for.