Hi bpat 1434 - NO array_merge_recursive() has the problem that if the two arrays share a node and one is an array and the other is a string, the string gets appended to the array in the other vs. having the comparison array's string node overwrite the reference array's "array" node.
The purpose of this function is to have an array representing, say, settings. Then declare separately another "custom user settings" array. If the 2nd array has any differences, they will overwrite the first array.
Additionally, array_merge_recursive() re-indexes keys in some cases. try running it on $a1 and $a2 below and you'll see.
I COULD STILL USE HELP ON THIS, NOTE THE LINE
if($str)
- I had to add that because on recursing, the whole array got overwritten. Uncomment the echo lines and you'll see what i mean.
My solution isn't "pretty" but it gives the results that a human being would understand and predict, I have included the code below:
<pre>
<?php
//this is the first array
$a1=array(
'key1'=>array(
'cats'=>array(
1=>'hello there',
2=>'18 signals',
3=>'inet buffer',
'podcast'=>'1 hour long'
)
),
'key2'=>array(
'cosmo'=>5,
'questions'=>'antidisestablishmentarianism',
5=>'cosmo',
'yec'=>array(
'three'=>1,
'blind'=>2
)
),
7=>'elegant'
);
//this is the second array which is merged into or onto the first
$a2=array(
'key3'=>array(
'not'=>'here',
'but'=>'added'
),
'key1'=>array(
'cats'=>'overwritten!',
'dogs'=>array(
'rule'=>'over cats'
)
),
'key2'=>array(
'questions'=>'short'
)
);
//these 2 vars are just for testing
$passcounter1=$passcounter2=0;
function array_merge_accurate(){
$arrays=func_get_args();
if(count($arrays)==0)return '';
if(count($arrays)==1)return $arrays[0];
//store the first array passed
$firstArrayIdentifier='a'.md5(time().rand(1,100000));
global $$firstArrayIdentifier;
$$firstArrayIdentifier=$arrays[0];
for($i=1; $i<=count($arrays); $i++){
array_merge_accurate_subroutine($firstArrayIdentifier, $arrays[$i]);
}
return $$firstArrayIdentifier;
}
function array_merge_accurate_subroutine($firstArrayIdentifier,$comparisonArray, $keys=array()){
global $passcounter1, $passcounter2, $$firstArrayIdentifier;
//where are we
if($keys){
$str='';
foreach($keys as $build)$str.='['.(is_numeric($build)?'':'\'').$build.(is_numeric($build)?'':'\'').']';
}
#echo( '<span style=color:darkred>$mylocation=$comparisonArray'.$str.';</span><br>' );
eval( '$mylocation=$comparisonArray'.$str.';' );
if(is_array($mylocation)){
$passcounter1++;
foreach($mylocation as $n=>$v){
#echo 'calling again '.$passcounter1.':' . $passcounter2.' with key <strong>'.$n.'</strong> and keys <strong>'.(implode('.',$keys) ? implode('.',$keys) : '-').'</strong><br />';
array_merge_accurate_subroutine($firstArrayIdentifier, $comparisonArray, array_merge($keys,array($n)));
}
}else{
$passcounter2++;
#echo('pass '.$passcounter1.':'.$passcounter2.' $'.$firstArrayIdentifier.$str.'='.(is_numeric($mylocation)?'':'"').str_replace('"','\"',$mylocation).(is_numeric($mylocation)?'':'"').';<br />');
if($str)
eval('$'.$firstArrayIdentifier.$str.'='.(is_numeric($mylocation)?'':'"').str_replace('"','\"',$mylocation).(is_numeric($mylocation)?'':'"').';');
}
}
$result=array_merge_accurate($a1,$a2);
print_r($result);
//this is the result of the merge
/*
print_r($result);
$result=array(
'key1'=>array(
'cats'=>'overwritten!',
'dogs'=>array(
'rule'=>'over cats'
)
),
'key2'=>array(
'cosmo'=>5,
'questions'=>'short',
5=>'cosmo',
'yec'=>array(
'three'=>1,
'blind'=>2,
'mice'=>3,
'see how they run'=>4
)
),
'key3'=>array(
'not'=>'here',
'but'=>'added'
),
7=>'elegant'
);
*/
?>