tags:

views:

65

answers:

1

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.

+1  A: 

PHP actually provides a function called array_merge_recursive() which is used exactly in this type of scenario. From the documentation page:

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Example:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

Output:

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)
Andrew Moore
thank you =) I glanced over array_merge_recursive() but upon first read it didn't seem like what I wanted. I stand corrected!
Derek Adair