Hi,
I have a query that I run several times with different parameters. I am using an xml parser to return the results to jQuery.
I can't seem to find a way to combine the results on the first 'node' without overwriting the first entry.
Simplified code sample:
$temp1 = returnArray(0);
$temp2 = returnArray(1);
$data = array_merge($temp1, $temp2);
print_r($data);
function returnArray($switch)
{
if($switch == 0)
{
$data['data']['first'] = "something";
} else {
$data['data']['second'] = "something ELSE";
}
return $data;
}
results in printing out Array ( [data] => Array ( [second] => something ELSE ) )
- $temp2 is overwriting $temp1. I understand this to be the default behavior of array_merge(), which is confusing to me.
I have also tried doing something like $data = $temp1+$temp2;
or...
$data = returnArray(0);
$data += returnArray(1);
prints Array ( [data] => Array ( [first] => something ) )
or...
$data = returnArray(0);
$data .= returnArray(1);
print_r($data);
prints ArrayArray
I ended up hacking this together:
$data['data'] = returnArray(0);
$data['data'] = returnArray(1);
function returnArray($switch){
if($switch == 0) return Array('first' => 'something');
else return Array('second' => 'something else');
}
Which i'm not all that happy with... Although it gets the job done in this case, it won't be all that applicable in the future when a more complex situation occurs.
Is there a better way to take two associative, multi-dimensional arrays and combine them without overwriting?
EDIT:
I'd like the method to be reusable, extensible and be able to handle 2+ arrays with 2+ dimensions.