October 15th, 2012, 03:19 PM
Merging Associative Arrays with Overwrite/Append
Alright, I've been around long enough where I should know how to do this by now. I have worked around this in the past by looping through both arrays and coming up with a third array that contains the appropriate values. So here is my test data...
I have tried array_merge and adding the arrays together. Adding the arrays appends the style which works but I can't figure out how to overwrite the 'id' value without looping through the array.
$defaults = Array(
$newArray = Array(
),'style' => Array(
//I want the defaults array to change beta to test and append
//the style on it... so the defaults array ends up looking like
Is there any tips on how to accomplish this more efficiently?
Last edited by pgrinPDT; October 15th, 2012 at 03:22 PM.
October 15th, 2012, 03:48 PM
I fear there is no built-in function for this. I thought of array_merge_recursive(), but this works very different (scalar values are joined in an array instead of overwritten).
So you'll either have to use a loop or recursion.
October 15th, 2012, 06:47 PM
I agree with Jacques, I don't believe there is any built in function with that exact logic. You could file it as a feature request against array_merge_recursive (as an optional parameter), it sounds like something that has a decent chance of getting added eventually.
October 16th, 2012, 11:54 AM