Well, as much as I dislike just giving code away, here's what I came up with.
<?php
$array1 = array(
'contactId' => 1,
'name' => array(
'firstName' => 'Fully',
'middleName' => 'its',
'lastName' => 'updated',
'prefix' => 'my prefix',
),
'work' => array(
'company' => 'my company',
'department' => 'my dept',
),
'emails' => array(
array('address' => 'my@mail.com'),
array('type' => 'home'),
),
'phones' => array(
array('number' => '2789015'),
),
'address' => array(
array(
'type' => 'home',
'formattedAddress' => array(
'city' => 'my city',
'street' => 'my street',
),
'isMailing' => 1,
),
),
'communication' => array(
'ims' => array(
array('address' => 'me@im.com'),
),
'webUrls' => array(
array('url' => 'http://www.mywebsite.com'),
),
),
'extras' => array(
'phoneticLastName' => 'my phonetic last name',
'dates' => array(
'date2' => '2008-09-09',
),
),
);
$array2 = array(
'contactId' => 4,
'lastUpdated' => '2008-09-09 07:06:15',
'name' => array(
'fullName' => 'Full Name',
'suffix' => 'suffix',
),
'work' => array(
'jobTitle' => 'job title',
),
'emails' => array(
array('type' => 'other'),
array('address' => 'gmail1@email.com'),
),
'phones' => array(
array('type' => 'work')
),
'address' => array(
array(
'type' => 'home',
'formattedAddress' => array(
'state' => 'guj1',
'zip' => '395008',
'countryCode' => 'US1',
),
'isMailing' => 1,
),
),
'communication' => array(
'ims' => array(
array('protocol' => 'GOOGLE'),
),
'webUrls' => array(
array('type' => 'home'),
),
),
'extras' => array(
'phoneticFirstName' => 'fname1',
'dates' => array(
'date1' => '2008-09-07'
),
),
'note' => '12122notes for this contact123',
);
/**
* Recursively merge two arrays together
*
* @param array $array1
* @param array $array2
* @param boolean $overwrite Defines whether non-array values in $array1 are
* overwritten by those in $array2
* @return array
*/
function myArrayMergeRecursive($array1, $array2, $overwrite=true)
{
foreach($array2 as $key=>$val)
{
if(isset($array1[$key]))
{
if(is_array($val))
$array1[$key] = myArrayMergeRecursive($array1[$key], $val);
elseif((is_string($array1[$key]) || is_int($array1[$key])) && $overwrite)
$array1[$key] = $val;
}
else
$array1[$key] = $val;
}
return $array1;
}
$array = myArrayMergeRecursive($array1, $array2);
print_r($array);
This gives me the following output:
---------- PHP - Execute ----------
Array
(
[contactId] => 4
[name] => Array
(
[firstName] => Fully
[middleName] => its
[lastName] => updated
[prefix] => my prefix
[fullName] => Full Name
[suffix] => suffix
)
[work] => Array
(
[company] => my company
[department] => my dept
[jobTitle] => job title
)
[emails] => Array
(
[0] => Array
(
[address] => my@mail.com
[type] => other
)
[1] => Array
(
[type] => home
[address] => gmail1@email.com
)
)
[phones] => Array
(
[0] => Array
(
[number] => 2789015
[type] => work
)
)
[address] => Array
(
[0] => Array
(
[type] => home
[formattedAddress] => Array
(
[city] => my city
[street] => my street
[state] => guj1
[zip] => 395008
[countryCode] => US1
)
[isMailing] => 1
)
)
[communication] => Array
(
[ims] => Array
(
[0] => Array
(
[address] => me@im.com
[protocol] => GOOGLE
)
)
[webUrls] => Array
(
[0] => Array
(
[url] => http://www.mywebsite.com
[type] => home
)
)
)
[extras] => Array
(
[phoneticLastName] => my phonetic last name
[dates] => Array
(
[date2] => 2008-09-09
[date1] => 2008-09-07
)
[phoneticFirstName] => fname1
)
[lastUpdated] => 2008-09-09 07:06:15
[note] => 12122notes for this contact123
)
Output completed (0 sec consumed)
Hope that helps.... I tried to document it pretty well...