Yeah. I was just thinking of making another function that used the array_merge function, but it would be really complicated and I'd have to implement a bunch of ifs or a switch...
It needs to check if the array exists inside both parent arrays, and if so then it needs to run itself again on that child array to see if it has any more dimensions. If not, then it just does an array merge.
Hmmm, lemmy see if I can explain this easier (for my sake as well as everyone elses).
function blah(array1,array2)
if keyA exists in array1 and keyA exists in array2
then blah(array1[keyA],array2[keyA])
else (if it doesn't exist in both arrays)
then array_merge(array1,array2)
The problem is finding the deapest dimension and going backwards from there to keep it from overwriting data...
I might write up a function and see if I can't get it to work. The logic is confusing me though, lol. That and I'm tired
[... Hours Go By ...]
Nope, I'm stumped... (head hurts too, I should go to bed... its already 0300
)
EDIT:
Heres what I wrote, for those who want to check it out and try and work with it.
Don't ask questions about it though, because I have no clue how half of this function works... I just let my brain do it, lol, while I day dreamed (happens allot!
Thats where most of my good code comes from
)
Code: <?php
session_start();
print "<pre>";
function array_merge_md($array1,$array2,$cleanup=true) {
$orig_array1 = $array1;
foreach($array1 as $k => $v) {
if($array1[$k]!=$array2[$k]) {
if(!empty($array1[$k])&&!empty($array2[$k])) {
if(is_array($array1[$k])&&is_array($array1[$k]))
$array1[$k] = array_merge_md($array1[$k],$array2[$k],false);
else
$array1[$k] = $array2[$k];
} elseif(empty($array1[$k])&&!empty($array2[$k]))
$array1[$k] = $array2[$k];
}
}
if($cleanup===true) $array1 = array_merge_md($array2,$array1,false);
return $array1;
}
$Array1 = array("SESSION"=>array("PUSH"=>array("STAGE"=>1,"AID"=>123456)));
$Array2 = array("SESSION"=>array("Error_ID"=>"","PUSH"=>array(0=>"PUSHED")));
print_r($Array1); print "<br /><br />";
print_r($Array2); print "<br /><br />";
$Array3 = array_merge_md($Array1,$Array2);
print_r($Array3); print "<br /><br />";
print "</pre>";
?>
The cleanup=true, I assume, means that it runs the function again except backwards and array1 has its new values.
array_merge_md == Multi-Dimensional
EDIT (again... teehee):
I wrote yet another function that did not work. I cleaned this one up a tad bit and I feel a little more concious (probably not, but eh )...
Code: function array_merge_md($array1,$array2,$first_time=true) {
foreach($array1 as $k => $v) {
if(array_key_exists($k,$array2)) {
if($array2[$k]!=$v) {
if(is_array($array2[$k])&&is_array($array1[$k]))
$array1 = array_merge_md($array1[$k],$array2[$k],false);
else
$array1[$k] = $array2[$k];
}
}
}
if($first_time===true) $array1 = array_merge_md($array2,$array1,false);
return $array1;
}