n3mesis125 Posted May 6, 2008 Share Posted May 6, 2008 Hey everyone, I'm trying to do a recursive merge with two arrays but it doesn't seem to be working properly. For whatever reason, the first array's value is being overwritten or being added onto compared to the second array. An example of data in two arrays is as follows: <?php $array1 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_today' => 1 ); $array2 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_prev' => 4 ); ?> I am using the below function to do a recursive_unique merge: <?php function array_merge_recursive_unique($array1, $array2) { // STRATEGY /* Merge array1 and array2, overwriting 1st array values with 2nd array values where they overlap. Use array1 as the base array and then add in values from array2 as they exist. Walk through each value in array2 and see if a value corresponds in array1. If it does, overwrite with second array value. If it's an array, recursively execute this function and return the value. If it's a string, overwrite the value from array1 with the value from array2. If a value exists in array2 that is not found in array1, add it to array1. */ // LOOP THROUGH $array2 foreach($array2 AS $k => $v) { // CHECK IF VALUE EXISTS IN $array1 if(isset($array1[$k])) { // IF VALUE EXISTS CHECK IF IT'S AN ARRAY OR A STRING if(!is_array($array2[$k])) { // OVERWRITE IF IT'S A STRING $array1[$k]=$array2[$k]; } else { // RECURSE IF IT'S AN ARRAY $array1[$k] = array_merge_recursive_unique($array1[$k], $array2[$k]); } } else { // IF VALUE DOESN'T EXIST IN $array1 USE $array2 VALUE $array1[$k]=$v; } } unset($k, $v); return $array1; } ?> The output result that I get from using array_merge_recursive_unique($array1, $array2) is: <?php $array1 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_today' => 9, 'count_prev' => 4 ); ?> However, it should be: <?php $array1 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_today' => 1, 'count_prev' => 4 ); ?> Not sure what I'm missing here, but for whatever reason it keeps changing the 1 to a 9? Any ideas... Thanks, n3m. Link to comment https://forums.phpfreaks.com/topic/104442-recursive-merge-php/ Share on other sites More sharing options...
rhodesa Posted May 6, 2008 Share Posted May 6, 2008 $array3 = array_merge($array1,$array2); Link to comment https://forums.phpfreaks.com/topic/104442-recursive-merge-php/#findComment-534639 Share on other sites More sharing options...
n3mesis125 Posted May 6, 2008 Author Share Posted May 6, 2008 array_merge works fine and dandy until you start throwing in more arrays, my array is much more extensive than the example I provided above, imagine if you had the below arrays: <?php $array1 = array( '0' => array( 'lob' => 'cable', 'tag' => 'ebpp', 'count_today' => 1 ), '1' => array( 'lob' => 'cable', 'tag' => 'test', 'count_today' => 2 ) ); $array2 = array( '0' => array( 'lob' => 'cable', 'tag' => 'ebpp', 'count_prev' => 4 ), '1' => array( 'lob' => 'cable', 'tag' => 'test', 'count_prev' => 6 ) ); ?> The result I would want from array_merge would be: <?php $new_array = array( '0' => array( 'lob' => 'cable', 'tag' => 'ebpp', 'count_today' => 1, 'count_prev' => 4 ), '1' => array( 'lob' => 'cable', 'tag' => 'test', 'count_today' => 2, 'count_prev' => 6 ) ); ?> Link to comment https://forums.phpfreaks.com/topic/104442-recursive-merge-php/#findComment-534666 Share on other sites More sharing options...
rhodesa Posted May 6, 2008 Share Posted May 6, 2008 um...i just ran the code from your original example and it worked fine... <?php function array_merge_recursive_unique($array1, $array2) { // STRATEGY /* Merge array1 and array2, overwriting 1st array values with 2nd array values where they overlap. Use array1 as the base array and then add in values from array2 as they exist. Walk through each value in array2 and see if a value corresponds in array1. If it does, overwrite with second array value. If it's an array, recursively execute this function and return the value. If it's a string, overwrite the value from array1 with the value from array2. If a value exists in array2 that is not found in array1, add it to array1. */ // LOOP THROUGH $array2 foreach($array2 AS $k => $v) { // CHECK IF VALUE EXISTS IN $array1 if(isset($array1[$k])) { // IF VALUE EXISTS CHECK IF IT'S AN ARRAY OR A STRING if(!is_array($array2[$k])) { // OVERWRITE IF IT'S A STRING $array1[$k]=$array2[$k]; } else { // RECURSE IF IT'S AN ARRAY $array1[$k] = array_merge_recursive_unique($array1[$k], $array2[$k]); } } else { // IF VALUE DOESN'T EXIST IN $array1 USE $array2 VALUE $array1[$k]=$v; } } unset($k, $v); return $array1; } $array1 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_today' => 1 ); $array2 = array( 'lob' => 'h', 'tag' => 'tag1', 'count_prev' => 4 ); print_r(array_merge_recursive_unique($array1,$array2)); ?> Array ( [lob] => h [tag] => tag1 [count_today] => 1 [count_prev] => 4 ) Link to comment https://forums.phpfreaks.com/topic/104442-recursive-merge-php/#findComment-534683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.