homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

$array1 + $array2 with multi-dimensional arrays

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

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)

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)

$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)

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)


Might have the answer you are looking for.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved